Introduction to PowerShell Test-Path
Powershell test-path command checks for the existence of the path for all the elements. It always returns boolean values. It will return $True and $False. Basically it will return $True as all the elements are there else it will return $False. With the help of Test-Path command, we can also identify the path types, that is if the path is a container or 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 we wanted to check if the path exists or not 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 if the path exists or not. It will simply validate the path syntax, so if the path syntax is valid it will return $True and 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 path like “ranjan.txt” which is an exact name not like matching. Good things here we can use escape characters also 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 than it will return true. Example take an argument passed as “August 13, 2019” and date of file creation is “August 15, 2019” than it will return true as file creation is newer than 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 argument date provided than it will return true. Example take an argument passed as “August 13, 2019” and date of file creation is “August 15,2019” than it will return false as an argument passed is newer than the creation date.
8. -Path: Defines any path which is going to be tested. We can also use a wildcard in this case. Also in case if the path has spaces in between them then 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 types which we defined in command than 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 below parameters like value for the PathType command.
- Any container: It contains elements like registry, directories.
- Lead Item: This element will not contain attributes like any file.
- Combination: It can be both also, which any container or any leaf.
Examples of PowerShell Test-Path
Below are the examples of PowerShell Test-Path:
Example #1
Below command is one example where we are checking if there are 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 are checking PathType for $PROFILE by passing arguments like Any, Leaf, 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 than it will return True and 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 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 Test-Path command can be used for either checking 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 Powershell test-path. You may also have a look at the following articles to learn more –