Return true if the path exists, otherwise return false, determines whether all elements of the path exist.

      Test-Path { [-path] string[] | [-literalPath] string[] }
            [-pathType TestPathType ] [-isValid]
               [-include string[]] [-exclude string[]] [-Filter string]
                  [-credential PSCredential] [-UseTransaction] [CommonParameters]

   -Path string[]
       The PowerShell path (or paths) to test
       Wildcards are permitted. {may be piped}

   -LiteralPath string
       Like Path above, only the value is used exactly as typed.
       No characters are interpreted as wildcards. If the path includes any
       escape characters then enclose the path in single quotation marks.

   -PathType TestPathType
       One of: Container, Leaf or Any
          Container  = The parent container of the item (parent folder)
          Leaf    = The last item or container in the path.(filename)
          Any     = Either a container or a leaf.

       Return TRUE if the path syntax is valid, even if elements of the path
       don't exist.

   -include string
       Test only the specified items from the Path. e.g. "May*"
       this only works when the path includes a wildcard character.

   -Exclude string
       Omit the specified items from the Path e.g. "*SS64*"
       this only works when the path includes a wildcard character.

   -Filter string
       A filter in the provider's format or language.
       The value of this parameter qualifies the Path parameter.
       The exact syntax of the filter (wildcard support etc) depends on the provider.
       Filters are more efficient than -include/-exclude, because the provider
       applies the filter when retrieving the objects, rather than having 
       PowerShell filter the objects after they are retrieved.

   -Credential PSCredential
       Use a credential to validate access to the file. Credential represents
       a user-name, such as "User64" or "Domain64\User64", or a PSCredential
       object, such as the one retrieved by using the Get-Credential cmdlet.
       If you type a user name, you will be prompted for a password.
       This parameter is not supported by any PowerShell core cmdlets or providers.

       Include the command in the active transaction.

       -Verbose, -Debug, -ErrorAction, -ErrorVariable, -WarningAction, -WarningVariable,
       -OutBuffer -OutVariable.

Test-Path will either return $true or $false.

Test-Path will correctly test for the presence or absence of a valid path string, but if you ask it to test a path which is $null or a Zero Length String it will return an error. You capture such null/empty variables with a Try statement like this:

$TestPath = $null

if ( $(Try { Test-Path $TestPath.trim() } Catch { $false }) ) {
   write-host "Path OK"
Else {
   write-host "Path not found"


The current version of Test-Path will always return $true for a path that consists of a single space " "
To prevent this trim() the string you pass to Test-Path.

Test-Path does not work correctly with all PowerShell providers. For example, you can use Test-Path to test the path to a registry key, but if you use it to test the path to a registry entry, it always returns FALSE, even if the registry entry is present.


Determine whether all elements (folders) in the path exist:

PS C:\> test-path "F:\MSSQL\BACKUP"

Determine whether there are any files in the Spreadsheets folder other than .xlsx files:

PS C:\> test-path C:\Spreadsheets\* -exclude *.xlsx

Determine whether the path stored in the $profile variable leads to a directory or a file:

PS C:\> test-path $profile -pathtype leaf

If a folder does not exist, then create it:

$folder = "F:\BACKUP"

if ( -Not (Test-Path $folder.trim() ))
 New-Item -Path $folder -ItemType Directory

“I don't want to sound like I'm bragging but I think I've finally managed to play the record at the right speed” ~ John Peel


Convert-Path - Convert a ps path to a provider path.
join-path - Combine a path and child-path
Resolve-Path - Resolves the wildcards in a path
Split-Path - Return part of a path.
Get-Help about_namespace
Equivalent bash command: test - Evaluate a conditional expression

Copyright © SS64.com 1999-2018
Some rights reserved