question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Test-Path does not throw an exception on invalid path

See original GitHub issue

Prerequisites

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:closed
  • Created 2 years ago
  • Comments:8

github_iconTop GitHub Comments

2reactions
jasonwoernercommented, Jul 11, 2023

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?

0reactions
msftbot[bot]commented, Nov 14, 2021

This issue has been marked as by-design and has not had any activity for 1 day. It has been closed for housekeeping purposes.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found