PowerShell for Windows Admins

Dec 19 2017   12:19PM GMT

Test-Path -PathType

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Tags:
Powershell

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.

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

Share this item with your network: