In PowerShell, can Test-Path (or something else) be used to validate multiple files when declaring a string array parameter

前端 未结 3 1864
萌比男神i
萌比男神i 2021-01-11 14:18

I have a function that accepts a string array parameter of files and I would like to use Test-Path (or something else) to ensure that all the files in the string array param

3条回答
  •  一整个雨季
    2021-01-11 14:43

    As @Matt said. Test-Path already accepts pipeline input, so you really just have to send the array directly in:

    @($path1, $path2) | Test-Path
    

    Which then returns:

    > @("C:\foo", "C:\Windows") | Test-Path
    False
    True
    

    If you just want to know if ALL of them exist:

    ($pathArray | Test-Path) -notcontains $false
    

    Which yields:

     False
    

提交回复
热议问题