Testing Files and Folders (separately)

by Nov 4, 2016

Test-Path is highly useful to test whether a file or folder exists, and it can be used with any of the PowerShell drives, so it can also test whether a variable, a function, or a certificate exists (for example).

In recent PowerShell versions, Test-Path can now differentiate between containers (i.e. folders) and leafs (i.e. files), too:

$path = 'c:\windows\explorer.exe'
# any item type
Test-Path -Path $path
# just files
Test-Path -Path $path -PathType Leaf
# just folders
Test-Path -Path $path -PathType Container

Twitter This Tip! ReTweet this Tip!