Test-Path will throw an error when the path is $null (or an empty string) instead of returning $false.
The errors that we do get are parameter binding errors
I find myself often running into errors with Test-Path because of empty or $null strings. Then I end up making my validation more complex to account for it.
if ( Test-Path -Path $Path)
{
# do something
}
# VS
if ( $null -ne $Path -and ( Test-Path -Path $Path) )
{
# do something
}
It would be nice if it would just return $false (or offer a -Quiet parameter)
Steps to reproduce
$path = Resolve-Path -Path '.\*\nothing'
Test-Path $path
Resolve-Path -Path '.\*\nothing' | Test-Path
Expected behavior
Actual behavior
Error: Test-Path : Cannot bind argument to parameter 'Path' because it is null.
Environment data
> $PSVersionTable
PSVersion 6.1.0
PSEdition Core
GitCommitId 6.1.0
OS Microsoft Windows 10.0.16299
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
Test-Path will throw an error when the path is
$null(or an empty string) instead of returning$false.The errors that we do get are parameter binding errors
I find myself often running into errors with
Test-Pathbecause of empty or$nullstrings. Then I end up making my validation more complex to account for it.It would be nice if it would just return
$false(or offer a-Quietparameter)Steps to reproduce
Expected behavior
Actual behavior
Environment data