Check multiple path with Test-path

后端 未结 1 963
心在旅途
心在旅途 2021-01-19 06:32

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 select

1条回答
  •  借酒劲吻你
    2021-01-19 06:52

    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
    

    0 讨论(0)
提交回复
热议问题