kekimian
kekimian

Reputation: 947

Check multiple path with Test-path

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

Answers (1)

Micky Balladelli
Micky Balladelli

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

Related Questions