Test-Path incorrect file validation

This is an expected behavior:

PS C:\> Test-Path -Path C:\MyFolder\ -IsValid -PathType Container
True
PS C:\>

However, this seems to be incorrect:

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

According to the official documentation:

"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

Is it a bug or did I miss something?

what version of PowerShell you are using ? I cannot repro this in PSCore 6.2.0-Preview.3

You should read the documentation more carefully:

-IsValid

Indicates that this cmdlet tests the syntax of the path, regardless of whether the elements of the path exist.

If you ommit the parameter -IsValid it should work as you probably expect it to work. :wink:

I tried with both PS 5.1 and 6.1

[quote quote=132935]You should read the documentation more carefully:

-IsValid

Indicates that this cmdlet tests the syntax of the path, regardless of whether the elements of the path exist.

If you ommit the parameter -IsValid it should work as you probably expect it to work.
[/quote]

That’s exactly what I want to do: validating the syntax regardless whether the object already exists or not.

I am using the IsValid parameter on purpose in order to check the syntax before trying to create a file.

This means for me that IsValid is not able to detect that C:\MyFolder</strong> is always a folder object and cannot be a leaf.

It seems to be like lit on StackOverflow already said - you cannot detect if it’s a container or a file if it’s not existing yet. :wink:

[quote quote=132959]It seems to be like lit on StackOverflow already said – you cannot detect if it’s a container or a file if it’s not existing yet.
[/quote]

Ok then if IsValid is not able to make the difference between a leaf and a container, then it doesn’t make sense to have both IsValid and PathType in the same parameter set and they should be mutually exclusive.

I am considering to open an issue on Github.

Might be a good idea, I suppose. But yeah, since a file can in principle simply not have any extension and still be a file, it’s impossible for the cmdlet to determine whether a given arbitrary path would be an invalid leaf.

Mind you, in Windows file systems </code> isn’t a valid filename character so it could try to use that, but then you run into issues with other file systems using different path-separators. Perhaps in combination with this PR it could use that to determine, however?

I don’t know if a final slash is making a difference between files and folders on a Linux system. Would be interesting to know…

Thanks everybody for helping me to figure this out.

Here is the final outcome:

According to Microsoft in Github issue #8607:

-IsValid and -PathType should have been in separate parameter sets
This has been referenced for a breaking change and the -PathType parameter has been appended with a Caution paragraph in the official documentation.

Cool. Well done you. Thanks a lot.