Monthly Archive

Categories

Test-Path -PathType

Test-Path is a useful cmdlet for testing the validity of a path. You get a True/False return depending on if the path exists or not.

 

There’s also a way to add to the test by determining if the path points to a container (folder) or a leaf (file) using the –PathType parameter.

 

Here’s some examples that show how it works:

PS> Test-Path -Path C:\Scripts\
True
PS> Test-Path -Path C:\Scripts\ -PathType Any
True
PS> Test-Path -Path C:\Scripts\ -PathType Container
True
PS> Test-Path -Path C:\Scripts\ -PathType Leaf
False
PS> Test-Path -Path C:\Scripts\foo.txt -PathType Container
False
PS> Test-Path -Path C:\Scripts\foo.txt -PathType Leaf
True

 

C:\Scripts is a folder and foo.txt in a file in that folder.

 

Test-Path –PathType is a good way to focus your test to ensure you’re dealing with a file or a folder as appropriate.

Comments are closed.