Test-Path
Test-Path [-Credential <PSCredential>] [-Exclude <String[]>] [-Filter <String>] [-Include <String[]>] [-IsValid]-LiteralPath* <String[]> [-NewerThan <DateTime>] [-OlderThan <DateTime>] [-Path*Type {Any | Container | Leaf}][-UseTransaction] [<CommonParameters>]
Test-Path [-Path*] <String[]> [-Credential <PSCredential>] [-Exclude <String[]>] [-Filter <String>] [-Include<String[]>] [-IsValid] [-NewerThan <DateTime>] [-OlderThan <DateTime>] [-PathType {Any | Container | Leaf}][-UseTransaction] [<CommonParameters>]
Test-Path [-NewerThan <DateTime>] [-OlderThan <DateTime>] [<CommonParameters>]
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.In a file system drive, Test-Path can tell whether a path is valid, whether all elements of the path exist, or report whether a path leads to a file or a directory. It can also tell whether a file was changed before or after a particular date.
Note: This custom cmdlet help file explains how the Test-Path cmdlet works in a file system drive. For information about the Test-Path cmdlet in all drives, type "Get-Help Test-Path -Path $null" or see Test-Path at http://go.microsoft.com/fwlink/?LinkID=113418.
Parameters
-OlderThan <DateTime>
Returns "True" when the LastWriteTime value of a file is less than the specified date. Otherwise, it returns "False". Enter a DateTime object, such as one that the Get-Date cmdlet returns, or a string that can be converted to a DateTime object, such as "August 10, 2011 2:00 PM".
OlderThan is a dynamic parameter that works only on file system paths. It was introduced in Windows PowerShell 3.0.
-NewerThan <DateTime>
Returns "True" when the LastWriteTime value of a file is greater than the specified date. Otherwise, it returns "False". Enter a DateTime object, such as one that the Get-Date cmdlet returns, or a string that can be converted to a DateTime object, such as "August 10, 2011 2:00 PM".
NewerThan is a dynamic parameter that works only on file system paths. It was introduced in Windows PowerShell 3.0.
-Credential <PSCredential>
- Default value is None
- Accepts pipeline input ByPropertyName
Specifies a user account that has permission to perform this action. The default is the current user.
Type a user name, such as User01 or Domain01\User01. Or, enter a PSCredential object, such as one generated by the Get-Credential cmdlet. If you type a user name, this cmdlet prompts you for a password.
This parameter is not supported by any providers installed with Windows PowerShell.
-Exclude <String[]>
- Default value is None
- Accepts pipeline input False
Specifies 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.
-Filter <String>
- Default value is None
- Accepts pipeline input False
Specifies 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 Windows PowerShell filter the objects after they are retrieved.
-Include <String[]>
- Default value is None
- Accepts pipeline input False
Specifies 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.
-IsValid [<SwitchParameter>]
- Default value is False
- Accepts pipeline input False
Indicates 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 is not.
-LiteralPath <String[]>
- This value is required
- Default value is None
- Accepts pipeline input ByPropertyName
Specifies a path to be tested. Unlike Path , the value of the LiteralPath parameter is used exactly as it is typed. No characters are interpreted as wildcard characters. If the path includes escape characters, enclose it in single quotation marks. Single quotation marks tell Windows PowerShell not to interpret any characters as escape sequences.
-Path <String[]>
- This value is required
- Default value is None
- Accepts pipeline input ByPropertyName
Specifies a path to be tested. Wildcard characters are permitted. If the path includes spaces, enclose it in quotation marks.
-PathType <TestPathType>
- Default value is None
- Accepts pipeline input False
Specifies 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 is 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 does not 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.
-UseTransaction [<SwitchParameter>]
- Default value is False
- Accepts pipeline input False
Includes the command in the active transaction. This parameter is valid only when a transaction is in progress.
<CommonParameters>
This cmdlet supports the common parameters: Verbose, Debug,ErrorAction, ErrorVariable, WarningAction, WarningVariable,OutBuffer, PipelineVariable, and OutVariable.
Inputs
System.StringOutputs
System.BooleanExamples
-
This command tells whether all elements in the path exist, that is,
the C: directory, the Documents and Settings directory, and the
NicoleH directory:
C:\PS> Test-Path -Path "C:\Documents and Settings\NicoleH"
If any are missing, the cmdlet returns FALSE. Otherwise, it returns TRUE.
-
These commands test the path to the Windows PowerShell profile:
C:\PS> Test-Path -Path $profile C:\PS> Test-Path -Path $profile -IsValid
The first command determines whether all elements in the path exist. The second command determines whether the syntax of the path is correct. In this case, the path is FALSE, but the syntax is correct (TRUE). These commands use $profile, the automatic variable that points to the location for the profile, even if the profile does not exist.
For more information about automatic variables, see about_Automatic_Variables.
-
This command tells whether there are any files in the Commercial
Buildings directory other than .dwg files:
C:\PS> Test-Path -Path "C:\CAD\Commercial Buildings\*" -Exclude *.dwg
The command uses the Path parameter to specify the path. Because it includes a space, the path is enclosed in quotes. The asterisk at the end of the path indicates the contents of the Commercial Building directory. (With long paths, like this one, type the first few letters of the path, and then use the TAB key to complete the path.)
The command uses the Exclude parameter to specify files that will be omitted from the evaluation.
In this case, because the directory contains only .dwg files, the result is FALSE.
-
This command tells whether the path stored in the $profile variable
leads to a file:
C:\PS> Test-Path -Path $profile -PathType Leaf
In this case, because the Windows PowerShell profile is a .ps1 file, the cmdlet returns TRUE.
-
These commands use the Test-Path cmdlet with the Windows PowerShell
registry provider:
C:\PS> Test-Path -Path HKLM:\Software\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell TRUE C:\PS> Test-Path -Path HKLM:\Software\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell\ExecutionPolicy FALSE
The first command tests whether the registry path to the Microsoft.PowerShell registry key is correct on the system. If Windows PowerShell is installed correctly, the cmdlet returns TRUE.
Test-Path does not work correctly with all Windows 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.
-
This command uses the NewerThan dynamic parameter to determine
whether the PowerShell.exe file on the computer is newer than July 13,
2009:
C:\PS> Test-Path $pshome\PowerShell.exe -NewerThan "July 13, 2009"
The NewerThan parameter works only in file system drives.
Additional Notes
The cmdlets that contain the Path noun (the Path cmdlets) work with path names and return the names in a concise format that all Windows PowerShell providers can interpret. They are designed for use in programs and scripts where you want to display all or part of a path name in a particular format. Use them as you would use Dirname , Normpath , Realpath , Join *, or other path manipulators. You can use the Path cmdlets with several providers, including the FileSystem, Registry, and Certificate providers. Test-Path is designed to work with the data exposed by any provider. To list the providers available in your session, type `Get-PSProvider`. For more information, see about_Providers. *
This work is licensed under a Creative Commons Attribution 4.0 International. It is attributed to Microsoft Corporation and can be found here.
PowerShell Commands