Skip to content

Test-Path -Path $null should return $false #8076

@KevinMarquette

Description

@KevinMarquette

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

False

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Enhancementthe issue is more of a feature request than a bugResolution-FixedThe issue is fixed.WG-Cmdlets-Managementcmdlets in the Microsoft.PowerShell.Management module

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions