Powershell: ErrorAction for Test-Path throw an NullReferenceException

Created on 27 Jan 2020  路  4Comments  路  Source: PowerShell/PowerShell

Steps to reproduce

#new PS session
# The variable 'Stop' not exist

Test-Path .\File.ps1 -ErrorAction $Stop

Expected behavior

message = Invalid value ?

Actual behavior

Test-Path: Cannot bind parameter 'ErrorAction' to the target.
Exception setting "ErrorAction": "Object reference not set to an instance of an object."

Environment data

```Powershell
Name                           Value
----                           -----
PSVersion                      7.0.0-rc.2
PSEdition                      Core
GitCommitId                    7.0.0-rc.2
OS                             Microsoft Windows 10.0.18362
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0, 5.0, 5.1.10032.0, 6.0.0, 6.1.0, 6.2.0, 7.0.0-rc.2}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Same issue with Windows PowerShell 5.1.

Issue-Question

All 4 comments

(As I'm sure that you're aware) the failure is expected, but the error message is confusing.

Sounds like testing for $null explicitly and using the same message you would get with [ValidateNotNull()], Cannot validate argument on parameter 'ErrorAction'. The argument is null. [...], would fix the problem.

(As I'm sure that you're aware)

Me yes, but not a beginner.
There is a difference between "it doesn't work" and "it doesn't work for such reason" :-)

It's not a really bug, just an improvement.

This won't just be about Test-Path either. -ErrorAction is implemented by the base Cmdlet class, so any improvement would need to be made there and would affect _all_ cmdlets.

But yes, applying such an attribute would make the error message much clearer.

:tada:This issue was addressed in #12124, which has now been successfully released as v7.1.0-preview.1.:tada:

Handy links:

Was this page helpful?
0 / 5 - 0 ratings