Contact Info

Crumbtrail » Administration » Powershell » Powershell 1.0 » Test-Path

Test-Path - Powershell 1.0 CmdLet

Microsoft Windows PowerShell is a command-line shell and scripting tool based on the Microsoft .NET Framework. It is designed for system administrators, engineers and developers to control and automate the administration of Windows and applications.

More than hundred command-line tools (so called "cmdlets") can be used to perform system administration tasks and Windows Management Instrumentation (WMI). These cmdlets are easy to use, with standard naming conventions and common parameters, and standard tools for piping, sorting, filtering, and formatting data and objects.


Return true if the path exists, otherwise return false


-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.

   -credential PSCredential
       Use a credential to validate access to the file. Credential represents
       a user-name, such as "User01" or "Domain01\User01", 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.

      -Verbose, -Debug, -ErrorAction, -ErrorVariable, -OutVariable.

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