Skip to content

Test-Path -IsValid -PathType Leaf doesn't correctly recognize a leaf #8607

@fullenw1

Description

@fullenw1

This issue is about an incorrect validation of the Test-Path cmdlet.

When a path ends with a backslash it is supposed to be a container.

Furthermore, according to the official documentation:

-IsValid
Indicates that this cmdlet tests the syntax of the path, regardless of whether the elements of the path exist. This cmdlet returns $True if the path syntax is valid and $False if it is not.

and also

"Leaf. An element that does not contain other elements, such as a file."

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/test-path?view=powershell-6

However, when using the -IsValid parameter combined to the -PathType Leaf parameter in order to validate the syntax of a file path, it is incorrectly validated as a leaf.

For example, the following command line returns $true instead of $false.

PS C:\> Test-Path -IsValid -PathType Leaf -Path 'C:\Something\'
True
PS C:\>

It should return false because a leaf path cannot end with a backslash.

This behavior has been tested on PowerShell 5.1 and 6.1.1.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Breaking-Changebreaking change that may affect usersIssue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aResolution-AnsweredThe question is answered.Resolution-ExternalThe issue is caused by external component(s).WG-Cmdlets-Corecmdlets in the Microsoft.PowerShell.Core module

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions