Return true if the path exists, otherwise return false, determines whether all elements of the path exist.
Syntax
Test-Path { [-path] string[] | [-literalPath] string[] }
[-pathType TestPathType ] [-isValid]
[-include string[]] [-exclude string[]] [-Filter string]
[-credential PSCredential] [-UseTransaction] [CommonParameters]
Key
-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.
-isValid
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.
-UseTransaction
Include the command in the active transaction.
CommonParameters:
-Verbose, -Debug, -ErrorAction, -ErrorVariable, -WarningAction, -WarningVariable,
-OutBuffer -OutVariable.
Test-Path will either return $true or $false.
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.
Examples
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
"The path of the righteous man is beset on all sides by the iniquities of the selfish and the tyranny of evil men. Blessed is he, who in the name of charity and good will, shepherds the weak through the valley of darkness, for he is truly his brother's keeper and the finder of lost children. And I will strike down upon thee with great vengeance and furious anger those who would attempt to poison and destroy my brothers. And you will know my name is the Lord when I lay my vengeance upon thee" - Ezekiel 25:17
Related:
Convert-Path cvpa 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