Updated June 13, 2023
Introduction to PowerShell Test-Path
Powershell test-path command checks for the path’s existence for all the elements. It always returns boolean values. It will return $True and $False. It will return $True as all the elements are there else; it will return $False. With the help of the Test-Path command, we can also identify the path types; that is, if the path is a container, leaf, or a terminal, it will always return $False if Path is whitespace, and it will return an error if the path is null. Real-Time uses, suppose you are checking any file in the directory, and you do not want to check, or you want to exclude one file from the directory because that particular file is in huge number, then you can use it.
Syntax 1:
Test-Path
[-Path] <String of path to be tested>
[-Filter <String of Filter arguments >]
[-Include <String of Path which need to include>]
[-Exclude <String of path which need to exclude>]
[-PathType <test path type(leaf, container or ny)>]
[-IsValid(return boolean true or false)]
[-Credential <PSCredential>]
[-OlderThan <DateTime to check older than>]
[-NewerThan <DateTime to check newer than>]
[<CommonParameters>]
Syntax 2:
Test-Path
-LiteralPath <String of exact path>
[-Filter <String of path to be tested>]
[-Include <String of Path which need to include>]
[-Exclude <String of path which need to exclude>]
[-PathType <test path type(leaf, container or ny>]
[-IsValid(return boolean true or false)]
[-Credential <PSCredential>]
[-OlderThan <DateTime to check older than>]
[-NewerThan <DateTime to check newer than>]
[<CommonParameters>]
Parameters
1. -Exclude: This command disallows or omits a defined item. This parameter will be used for path parameters. We can define path element or any pattern like “*.txt,””*.pdf,” etc. And these defined patterns can be excluded from the path.
2. -Filter: Allow our command to define a filter for checking path elements. For example, if we have huge file systems and want to check whether the path exists, we can define some filter conditions. We can use Wildcard for it.
3. -Include: This command we use to check for specific path attributes. In general, it will include the attribute passed into this command. Wildcard string attributes are also allowed in this case. We can use path element or attribute like “*.txt”,”*.pdf” etc.
4. -IsValid: It checks for the path syntax; this command does not worry about whether the path exists. It will simply validate the path syntax, so if the path syntax is valid, it will return $True; if the path syntax is not valid, it will return $False.
5. -LiteralPath: This is also one kind of path checking, but here, in this case, we have to pass the exact match path; we can not use “*.txt”; we must pass a path like “ranjan.txt,” which is an exact name not like matching. The good thing is that we can also use escape characters in these cases. In the case of escape characters, we should use single quotation marks. As a single quotation, inform PowerShell to treat characters as escape characters.
6. -NewerThan: Defines any time as DateTime; simply, it will check for the file creation dates, it checks if the date of the file creation date, and if the date of creation is newer than the argument date provided, then it will return true. For example, take an argument passed as “August 13, 2019,” and the date of file creation is “August 15, 2019” Then it will return true as file creation is newer than the argument pass date.
7. -OlderThan: Defines any time as DateTime; simply, it will check for the file creation dates; it checks if the date of file creation date, and if the date of creation is older than the argument date provided, it will return true. For example, take an argument passed as “August 13, 2019,” and the date of file creation is “August 15, 2019,” then it will return false as an argument passed as newer than the creation date.
8. -Path: Defines any path that will be tested. We can also use a wildcard in this case. Also, if the path has spaces between them, we can use a single quotation to inform PowerShell.
9. -PathType: It defines the exact types of the given element in the path. In simple it will check for paths elements types. It will return a boolean value. If the path of a given element is of the same type we defined in the command, then it will return $True, and if the type of path is not the same as what we defined in the command, then it will return $False. This command will take the parameters below, like the PathType command value.
- Any container: It contains elements like registry and directories.
- Lead Item: This element will not contain attributes like any file.
- Combination: It can be both also, which is any container or any leaf.
Examples of PowerShell Test-Path
Below are the examples of PowerShell Test-Path:
Example #1
The below command is one example where we check for any files inside “ranjan1” directories other than “*.txt.”
Test-Path -Path "./ranjan1/" -Exclude *.txt
Output:
Test-Path -Path "./ranjan1/" -Exclude *.pdf
Output:
Test-Path -Path "./ranjan1/" -Include *.pdf
Output:
cd ./ranjan1/
ls
Output:
Example #2
Below is an example for checking PathType. We contain PathType for $PROFILE by passing arguments like Any, Leaf, and Container.
Test-Path -Path $PROFILE -PathType Any
Output:
Test-Path -Path $PROFILE -PathType Container
Output:
Test-Path -Path $PROFILE -PathType leaf
Output:
Example #3
If the path is there, then it will return True; if the path does not exist, it will return False.
Test-Path -Path "./ranjan1/"
Output:
Test-Path -Path "./ranjan2/"
Output:
Test-Path -Path "./ranjan3/"
Output:
ls
Output:
Example #4
Here we are checking the file’s date of creation. It could be older or newer. I have created the file test.txt, it was created in 2019 before August month, and we are checking it with various dates bypassing them.
Test-Path ./test2.txt -NewerThan "August 13, 2019"
Output:
Test-Path ./test2.txt -OlderThan "August 13, 2019"
Output:
Test-Path ./test2.txt -NewerThan "August 13, 2020"
Output:
Test-Path ./test2.txt -NewerThan "Jan 13, 2020"
Output:
Test-Path ./test2.txt -NewerThan "July 13, 2020"
Output:
ls
Output:
Conclusion – PowerShell Test-Path
From above all, we learned that the Test-Path command can be used to either check path type or to check the path syntax. We can identify if the path is a container, leaf, or mixed(Any).
Recommended Articles
This has been a guide to PowerShell Test-Path. Here we discuss the introduction, parameters, and examples of the Powershell test path. You may also have a look at the following articles to learn more –