Reputation: 947
In my function i have 3 no mandatory and no positional parameters Folder1, Folder2 and Folder3
, before running my function i would like to check for each selected parameter if folder exist. It must be something dynamic because user can specify randomly one or two or all tree folders.
Thanks in advance.
Upvotes: 6
Views: 14142
Reputation: 10011
You can pass an array to Test-Path
Test-Path c:, d:, e:
True
True
True
EDIT
So I believe from your comment that you have something like this:
$mypaths = "c:","d:",$null
Test-Path $mypaths
Which issues the following error:
Test-Path : Cannot bind argument to parameter 'Path' because it is null.
Then you can try this:
$mypaths = "c:","d:",$null
$mypaths | Foreach { if ($_){Test-Path $_}}
Which results in only two valid paths.
So you could consider checking the returned values:
$valid = $mypaths | Foreach { if ($_){Test-Path $_}}
write-host "Variable `$mypaths contains $($mypaths.count) paths, and $($valid.count) were found valid"
Which outputs:
Variable $mypaths contains 3 paths, and 2 were found valid
Upvotes: 8