TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

PowerShell Test-Path

PowerShell Test-Path with What is PowerShell, History of PowerShell, Features of PowerShell, PowerShell vs CMD, PowerShell Scripting, PowerShell Versions, PowerShell Commands, PowerShell Looping, PowerShell Conditions, PowerShell Array, PowerShell Brackets etc.

<< Back to POWERSHELL

PowerShell Test-Path

The Test-Path cmdlet in PowerShell returns $true if all the elements of a file or directory path exist, otherwise $false. It can also tell whether the file was updated after or before the particular date. It determines whether the syntax of a path is valid or not.

Syntax

Test-Path 
[-Credential <PSCredential>] 
[-Exclude <String[]>] 
[-Filter <String>] 
[-Include <String[]>] 
[-IsValid]
-LiteralPath <String[]> 
[-NewerThan <DateTime>] 
[-OlderThan <DateTime>] 
[-PathType {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>]

Parameters

Following are the parameters used in the Test-Path cmdlet:

-Exclude

This parameter is used to specify those items which are omitted by this cmdlet. Wildcard characters are accepted in this parameter.

-Path

This parameter is used to specify the path to be tested. If you use the path which includes the spaces, enclose it in quotation marks. Wildcard characters are accepted in this parameter.

-Filter

This parameter is used to specify a filter in the format or language of the provider.

-Include

This parameter is used to specify the path which is tested by this cmdlet. In this parameter, the wildcard characters are accepted.

-IsValid

-IsValid is used to test the syntax of a path instead of checking the elements of a path. When this parameter is used in the cmdlet, the cmdlet returns the $True if the syntax of a path is valid, otherwise $False.

-LiteralPath

This parameter is used to specify the path to be tested. Unlike the -Path parameter, the value of this parameter is used exactly as it is typed.

-NewerThan

This parameter is used to specify the time as an object of DateTime.

-OlderThan

This parameter is also used to specify the time as an object of DateTime.

-PathType

This parameter is used to determine the type of final element which is specified in the path. When this parameter is used in the cmdlet, it returns $True if the element is of a specified type, otherwise returns $False.

For this parameter, following are the acceptable values:

  • Leaf: It is an element or a value which does not contain the other elements.
  • Container: It is an element or a value which contains the other elements, such as registry key or directory.
  • Any: It is an element which is either a leaf or a container.

Examples:

Example 1: Check the path in the file system.

PowerShell Test-Path

The command in this example checks whether all the elements in the path of file system exist or not. If any of the elements are missing in the path, then the command returns $False, otherwise $True. In this example, all elements in the path are correct, so it returns True.

Example 2: Check the path in the registry key

PowerShell Test-Path

The command, in this example, checks whether the path of Microsoft.PowerShell registry key is correct or not. If it is correct, then the command returns $True, otherwise $False.

Example 3: Test a file is older than the specified date

PowerShell Test-Path

Both the commands in this example uses the -olderThan parameter to check whether the k.txt file is older than the specified date.

The first command returns $False because it is last created or modified after the 10 December 2019. And the second command returns $True because it is last created or modified before the 15 December 2019.

The -olderThan parameter is used only with the file system.






Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf