Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8145327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:37:50+00:00 2026-06-06T13:37:50+00:00

this is my first time asking a question so bear with me. I am

  • 0

this is my first time asking a question so bear with me. I am teaching myself powershell by writing a few basic maintenance scripts. My question is in regard to a clean up script I am writing which accepts arguments to determine the target directory and files to delete.

The Problem:

The script accepts an optional argument for a list of file extensions to look for when processing the deletion of files. I am trying to test for the existence of the files prior to actually running the delete. I use test-path with the –include parameter to run the check within a ValidateScript block. It works if I pass in a single file extension or no file extensions, however when I try to pass in more than one file extension it fails.

I have tried using the following variations on the code inside the script:

[ValidateScript({ Test-Path $targetDirChk  -include $_ })]

[ValidateScript({ Test-Path $targetDirChk  -include "$_" })]

[ValidateScript({ Test-Path $targetDirChk  -include ‘$_’ })]

For each of the above possibilities I have run the script from the command line using the following variations for the multi extension file list:

& G:\batch\DeleteFilesByDate.ps1 30 G:\log  *.log,*.ext

& G:\batch\DeleteFilesByDate.ps1 30 G:\log  “*.log, *.ext”

& G:\batch\DeleteFilesByDate.ps1 30 G:\log  ‘*.log, *.ext’

Example of the error message:

chkParams : Cannot validate argument on parameter 'includeList'. The " Test-Path $targetDirChk -include "$_" " validation script for the argument with value "*.log, *.ext" did not return true. Determine why the validation script failed and then try the command again.
At G:\batch\DeleteFilesByDate.ps1:81 char:10
+ chkParams <<<<  @args
    + CategoryInfo          : InvalidData: (:) [chkParams], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,chkParams

The full script is below. I have not yet added the actual code to delete files, because I am still working on accepting and validating the arguments passed in.

I have searched google and stackoverflow but I have not found a solution to this particular problem. I assume I am either doing something wrong with the code, or there is a better way to accomplish what I want to do.

Note:
I should mention that I also tried running the test-path with multiple file extensions outside of the script with no problems:

PS G:\batch\powershell> test-path G:\log\* -include *.log

True

PS G:\batch\powershell> test-path G:\log\* -include *.log, *.ext

True

Script:

# Check that the proper number of arguments have been supplied and if not provide usage statement.
# The first two arguments are required and the third is optional.
if ($args.Length -lt 2 -or $args.Length -gt 3 ){
    #Get the name of the script currently executing.
    $ScriptName = $MyInvocation.MyCommand.Name
    $ScriptInstruction = @"

    usage: $ScriptName <Number of Days> <Directory> [File Extensions]

    This script deletes files from a given directory based on the file date.

    Required Paramaters:

    <Number of Days>:   
    This is an integer representing the number of days worth of files 
    that should be kept. Anything older than <Number of Days> will be deleted.

    <Directory>:        
    This is the full path to the target folder.

    Optional Paramaters:

    [File Extensions]   
    This is the set of file extensions that will be targeted for processing. 
    If nothing is passed all files will be processed.
"@  
    write-output $ScriptInstruction
    break
}
#Function to validate arguments passed in.
function chkParams()
{
    Param(
    [Parameter(Mandatory=$true,
        HelpMessage="Enter a valid number of days between 1 and 999")]

    #Ensure the value passed is between 1 and 999.
    #[ValidatePattern({^[1-9][0-9]{0,2}$})]
    [ValidateRange(1,999)]
    [Int]
    $numberOfDays,

    [Parameter(Mandatory=$true,
        HelpMessage="Enter a valid target directory.")]
    #Check that the target directory exists.
    [ValidateScript({Test-Path $_ -PathType 'Container'})] 
    [String]
    $targetDirectory,   

    [Parameter(Mandatory=$false,
        HelpMessage="Enter the list of file extensions.")]  
    #If the parameter is passed, check that files with the passed extension(s) exist.   
    [ValidateScript({ Test-Path $targetDirChk -include "$_" })]
    [String]
    $includeList
    )
    #If no extensions are passed check to see if any files exist in the directory.
    if (! $includeList ){
        $testResult = Test-path $targetDirChk
        if (! $testResult ){
            write-output "No files found in $targetDirectory"
            exit
        }
    } 
}
#
if ($args[1].EndsWith('\')){
    $targetDirChk = $args[1] + '*'
} else {
    $targetDirChk = $args[1] + '\*'
}       
chkParams @args
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T13:37:52+00:00Added an answer on June 6, 2026 at 1:37 pm

    -Include on Test-Path is a string[]. You probably want to mirror that definition:

    [ValidateScript({ Test-Path $targetDirChk -include $_ })]
    [String[]]
    $includeList
    

    And drop the "" from there because they will force the argument to be a string and thus trying to match a file that looks like `foo.log blah.ext.

    You also have to either put parentheses around that argument when calling the function or remove the space.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my first time asking a question in this website so please bear
this is my first time asking a question here. I tried to be well
this is my first time asking a question on stackoverflow. I'm working on a
First of all this is the first time I am asking a java question
First time using this service for a question. I hope I am not asking
This is my first time asking a question, please be gentle! I have a
This is my first time asking my question at stackoverflow. I'm working on a
Hi this is the first time I am asking a question here. I have
I'm asking this question because it isn't the first time I saw this coding
this is my first time asking a question here so apologies if I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.