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[]]
[-credential PSCredential] [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.
-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.
CommonParameters:
-Verbose, -Debug, -ErrorAction, -ErrorVariable, -OutVariable.
Test-Path will either return $true or $false.
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