Test-Path does not throw an exception on invalid path
See original GitHub issuePrerequisites
- Write a descriptive title.
- Make sure you are able to repro it on the latest released version
- Search the existing issues.
- Refer to the FAQ.
- Refer to Differences between Windows PowerShell 5.1 and PowerShell.
Steps to reproduce
Given this code:
function IsPathValid {
Param ([string]$Path)
try {
if (Test-Path -Path $Path -ErrorAction Stop) {
Write-Host "path $Path exists"
} else {
Write-Host "path $Path does not exist"
}
} catch {
Write-Host "caught error on $Path"
}
}
$Paths = @(
'C:\src\t'
,'C:\sr|c\t'
)
foreach ($Path in $Paths) { Write-Host "calling for $Path"; IsPathValid -Path $Path }
On Windows PowerShell 5.1, an invalid path given to Test-Path
produces an exception.
PS C:\src\t> $PSVersionTable.PSVersion.ToString()
5.1.19041.1320
PS C:\src\t> .\IsPathValid.ps1
calling for C:\src\t
path C:\src\t exists
calling for C:\sr|c\t
caught error on C:\sr|c\t
PS C:\src\t>
However, on PowerShell Core 7.2, an exception is not thrown.
PS C:\src\t> $PSVersionTable.PSVersion.ToString()
7.2.0
PS C:\src\t> .\IsPathValid.ps1
calling for C:\src\t
path C:\src\t exists
calling for C:\sr|c\t
path C:\sr|c\t does not exist
PS C:\src\t>
Expected behavior
PowerShell 7.2 should throw an exception when an invalid path is used with `Test-Path`.
Actual behavior
PowerShell 7.2 does not throw an exception when an invalid path is used with `Test-Path`.
Error details
No response
Environment data
PS C:\src\t> $PSVersionTable
Name Value
---- -----
PSVersion 7.2.0
PSEdition Core
GitCommitId 7.2.0
OS Microsoft Windows 10.0.19043
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
Visuals
No response
Issue Analytics
- State:
- Created 2 years ago
- Comments:8
Top Results From Across the Web
How to test for an invalid path in powershell?
So I use test-path to verify that the executable is where it is supposed to be. If not I try another location. if(test-path...
Read more >PowerShell Paths and Errors
Here are some techniques for testing paths in PowerShell and generating terminating exceptions if they are not found.
Read more >Execute-Process and invalid path - General Discussion
The log state the path is valid, which it is not. I'm OK with using Test-Path before the command, but just wondering is...
Read more >Why doesn't PowerShell Test-Path work with "\\?\" prefix ...
I have tried a couple of things that evaluate to true while still raising an error. Test-path -literalpath \\?\E:\ True.
Read more >[SOLVED] Test-path not working - PowerShell
I'm trying to test a path that I can visually see both in explorer ... Test-Path is not going to throw an error,...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
This is a confusing determination. Is it by-design that the Test-Path call using the -IsValid parameter will not provide an accurate result? It returns true in all cases. The call using IsValid does not attempt to validate the path. What is the point of the IsValid parameter and why are we accepting its useless existence?
This issue has been marked as by-design and has not had any activity for 1 day. It has been closed for housekeeping purposes.