Powershell check if folder exists

From Svendsen Tech PowerShell Wiki
Jump to: navigation, search

In this little article I describe how to use the cmdlet Test-Path to check if a folder exists. Type Get-Help Test-Path for built-in information. I also briefly demonstrate how to use the .NET class method Exists() from the class System.IO.Directory.

How To Check If A Folder Exists With PowerShell

You can use something like this for verification on the command line:

PS C:\> Test-Path C:\Windows

Remember that you need single or double quotes around the path if it contains a space. Single quotes are recommended, since they don't expand/substitute/interpolate variables.

To explicitly make sure it's a directory and not a file, use the -PathType parameter which accepts the following values:

  • Any
  • Leaf (file)
  • Container (directory/folder)
PS C:\> Test-Path C:\Windows -PathType Any
PS C:\> Test-Path C:\Windows -PathType Container
PS C:\> Test-Path C:\Windows -PathType Leaf

Screenshot Example


Script Usage

In a script, you would typically use it in an if statement. To negate and check if the folder or file does not exist, use either "!" or "-not", and remember to enclose the Test-Path statement in parentheses.

Also remember that if the path or folder name contains a space, you need to surround the entire path in quotes. Single quotes or double quotes will work the same if there are no "expandable" parts in the path or folder name, but the slightly safer choice is single quotes. This is what PowerShell defaults to when you auto-complete names with tab at the prompt.

PS C:\> if ( Test-Path 'C:\Windows' -PathType Container ) { "It's a container/folder/directory" }
It's a container/folder/directory
PS C:\> if ( -not (Test-Path 'C:\Windows' -PathType Leaf) ) { "It's not a leaf/file" }
It's not a leaf/file

Enumerating Possible PathType Values

A small "trick" to see the possible enumeration values for -PathType is to use one that doesn't exist, like this:

PS C:\> Test-Path C:\Windows -PathType foo
Test-Path : Cannot bind parameter 'PathType'. Cannot convert value "foo" to type
"Microsoft.PowerShell.Commands.TestPathType" due to invalid enumeration values.
Specify one of the following enumeration values and try again.
The possible enumeration values are "Any, Container, Leaf".
At line:1 char:31
+ Test-Path C:\Windows -PathType <<<<  foo
    + CategoryInfo          : InvalidArgument: (:) [Test-Path], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.TestPathCommand

Using The .NET System.IO.Directory Class Method "Exists"

You can also use the Exists() method from the .NET System.IO.Directory class, which requires a full path:

PS E:\temp> [System.IO.Directory]::Exists('E:\temp\')
PS E:\temp> [System.IO.Directory]::Exists('E:\temp')

If you want to check if the directory the script/program is currently in contains a subdirectory, you can use the trick I demonstrate below - where I check if there's a subdirectory called "Windows".

In PowerShell, the namespace "System" doesn't have to be typed in explicitly, so you can omit it.

PS C:\> [IO.Directory]::Exists( (Join-Path (Get-Location) 'Windows') )

PS C:\> cd E:\temp

PS E:\temp> [IO.Directory]::Exists( (Join-Path (Get-Location) 'Windows') )