Contact Info

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

Test-Path - Powershell 1.0 CmdLet

ActiveXperts Network Monitor ships with integrated Powershell scripts to monitor complex network. The scripts run out of the box
Download the ActiveXperts Network Monitor FREE version now »


Return true if the path exists, otherwise return false

Test-Path { [-path] string[] | [-literalPath] string[] }
            [-pathType TestPathType ] [-isValid]
               [-include string[]] [-exclude string[]]
                  [-credential PSCredential] [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.

   -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