CmdID / PowerShell / Cmdlets / Test
Test-Path
Determines whether all elements of a path exist. PowerShell reference
From the PowerShell reference 11 parameters
What it does
Determines whether all elements of a path exist. The Test-Path cmdlet determines whether all elements of the path exist. It returns $true if all elements exist and $false if any are missing. It can also tell whether the path syntax is valid and whether the path leads to a container or a terminal or leaf element. If the Path is a whitespace or empty string, then the cmdlet returns $false. If the Path is $null, an array of $null or an empty array, the cmdlet returns a non-terminating error.
Test-Path [-Path] <string[]> [-Filter <string>] [-Include <string[]>] [-Exclude <string[]>] [-PathType <TestPathType>] [-IsValid] [-Credential <pscredential>] [-UseTransaction] [-OlderThan <datetime>] [-NewerThan <datetime>] [<CommonParameters>]Test-Path -LiteralPath <string[]> [-Filter <string>] [-Include <string[]>] [-Exclude <string[]>] [-PathType <TestPathType>] [-IsValid] [-Credential <pscredential>] [-UseTransaction] [-OlderThan <datetime>] [-NewerThan <datetime>] [<CommonParameters>]Test-Path [-Path] <string[]> [-Filter <string>] [-Include <string[]>] [-Exclude <string[]>] [-PathType <TestPathType>] [-IsValid] [-Credential <pscredential>] [-UseTransaction] [<CommonParameters>]Test-Path -LiteralPath <string[]> [-Filter <string>] [-Include <string[]>] [-Exclude <string[]>] [-PathType <TestPathType>] [-IsValid] [-Credential <pscredential>] [-UseTransaction] [<CommonParameters>]
Test a path
Test-Path -Path "C:\Documents and Settings\DavidC"IdentifyTest the path of a profile
Test-Path -Path $PROFILEIdentifyCheck whether there are any files besides a specified type
Test-Path -Path "C:\CAD\Commercial Buildings\*" -Exclude *.dwgIdentifyCheck for a file
Test-Path -Path $PROFILE -PathType LeafIdentifyCheck paths in the Registry
Test-Path -Path "HKLM:\Software\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell"IdentifyTest if a file is newer than a specified date
Test-Path $PSHOME\powershell.exe -NewerThan "July 13, 2009"IdentifyTest a path with null as the value
Test-Path $null
Test-Path $null, $null
Test-Path @()IdentifyTest a path with whitespace as the value
Test-Path ' '
Test-Path ''IdentifyTest a path that may have an invalid drive
Test-Path -IsValid Z:\abc.txt
Test-Path -IsValid FileSystem::Z:\abc.txtIdentifyIts page in the reference: Test-Path.
Parameters
-CredentialNOTE: This parameter isn't supported by any providers installed with PowerShell. To impersonate another user, or elevate your credentials when running this cmdlet, use Invoke-Command.-ExcludeSpecifies items that this cmdlet omits. The value of this parameter qualifies the Path parameter. Enter a path element or pattern, such as *.txt. Wildcard characters are permitted.-FilterSpecifies a filter in the format or language of the provider. The value of this parameter qualifies the Path parameter. The syntax of the filter, including the use of wildcard characters, depends on the provider. Filters are more efficient than other parameters, because the provider applies them when it retrieves the objects instead of having PowerShell filter the objects after they're retrieved.-IncludeSpecifies paths that this cmdlet tests. The value of this parameter qualifies the Path parameter. Enter a path element or pattern, such as *.txt. Wildcard characters are permitted.-IsValidIndicates that this cmdlet tests the syntax of the path, regardless of whether the elements of the path exist. This cmdlet returns $true if the path syntax is valid and $false if it's not. If the path being tested includes a drive specification, the cmdlet returns false when the drive does not exist. PowerShell returns false because it doesn't know which drive provider to test.-LiteralPathSpecifies a path to be tested. Unlike Path, the value of the LiteralPath parameter is used exactly as it's typed. No characters are interpreted as wildcard characters. If the path includes characters that could be interpreted by PowerShell as escape sequences, you must enclose the path in single quote so that they won't be interpreted.-NewerThanThis is a dynamic parameter made available by the FileSystem provider.
Specify a time as a DateTime object.
Before PowerShell 7.5, the cmdlet ignores:
- This parameter when you specify PathType as any value other than Any.
- The OlderThan parameter when used with this parameter.
- This parameter when Path points to a directory.
Starting with PowerShell 7.5, you can use this parameter with any value for the PathType parameter, to test a date range with the OlderThan parameter, and to test the age of directories.
For more information, see about_FileSystem_Provider.-OlderThanThis is a dynamic parameter made available by the FileSystem provider.
Specify a time as a DateTime object.
Before PowerShell 7.5, the cmdlet ignores:
- This parameter when you specify PathType as any value other than Any.
- This parameter when used with the NewerThan parameter.
- This parameter when Path points to a directory.
Starting with PowerShell 7.5, you can use this parameter with any value for the PathType parameter, to test a date range with the NewerThan parameter, and to test the age of directories.
For more information, see about_FileSystem_Provider.-PathSpecifies a path to be tested. Wildcard characters are permitted. If the path includes spaces, enclose it in quotation marks.-PathTypeSpecifies the type of the final element in the path. This cmdlet returns $true if the element is of the specified type and $false if it's not. The acceptable values for this parameter are:
- Container - An element that contains other elements, such as a directory or registry key.
- Leaf - An element that doesn't contain other elements, such as a file.
- Any - Either a container or a leaf.
Tells whether the final element in the path is of a particular type.
CAUTION: Up to PowerShell version 6.1.2, when the IsValid and PathType switches are specified together, the Test-Path cmdlet ignores the PathType switch and only validates the syntactic path without validating the path type. According to issue #8607, fixing this behavior may be a breaking change in a future version, where the IsValid and PathType switches belong to separate parameter sets, and thus, can't be used together avoiding this confusion.-UseTransactionIncludes the command in the active transaction. This parameter is valid only when a transaction is in progress. For more information, see about_Transactions