The article aims to explore the main functionality of the PowerShell Test-Path.
How to use Test-Path cmdlet in PowerShell?
Test-Path cmdlet is used to check whether all path components are present. It returns the output in boolean i.e., True, False. The examples are as follows to describe the PowerShell Test-Path command.
Example 1: Test the Path of a directory or file in PowerShell
The below example will explain how to check your directory path in PowerShell. With the help of the Test-Path cmdlet, you can check whether your directory in the local drive exists or not.
The absolute path of the file/directory is passed to the Test-Path for path testing.
In the above example, the test path command helps to show if the picture folder does exist. As you can see, the Pictures folder part returns as True, which means the path exists.
Example 2: Check the Path Type of a file or directory
In PowerShell, you can also test the path type with the three acceptable values. These values are the leaf (a file), container (a folder/directory), or any (can be a file or directory). Suppose you want to check the test type of the Picture folder and guess that it might be “Leaf”.
$testPath = Test-Path $testPath -PathType Leaf
Clear-Host
$testPath
The above output shows that the picture folder is not a file (leaf) but rather a folder (container) that’s why the result is displayed as False.
We want to check the path type with the container option with the same code. Let’s see what the result will be.
$testPath = Test-Path $testPath -PathType Container
Clear-Host
$testPath
In the above code, the file/folder path is assigned to variable $testPath. Now, with the -PathType parameter along its value “Container” passed to the Test-Path to call check the path of the file/directory.
As you can see the path type of the Picture folder as the “Container” is true, meaning that the picture folder is a container rather than the “Leaf”.
Example 3: Test the Path of a Specific File
During path testing, you can also include/exclude parameters to test some options like file extensions to check whether these options also exist in the folder or not. In the below example, we will test with the -Include parameter.
$testPath = Test-Path $testPath -Include *.png, *.bmp, *.jpg
Clear-Host
$testPath
In the above code, the path to be tested is stored in the $testPath variable. Furtherly, this $testPath variable is passed to the Test–Path cmdlet. The Test-Path cmdlet includes only “.png”, “.bmp”, and “.jpg
” Files.
You can see that the path is True, which shows that the picture folder contains .JPG, .BMP, and .PNG files.
You can exclude files like .JPG, .PNG, and .BMP with the -Exclude parameter. You can also find if there are other file extensions available besides the given options.
$testPath = Test-Path $testPath -Exclude *.png, *.bmp, *.jpg
Clear-Host
$testPath
The “-Exclude” parameter of the Test-Path is used to check the files with their extensions on the path. -Exclude accepts the extension of the files as a value.
The output shows that the Test-Path has returned false, which states that the targeted directory does not contain files other than JPG, PNG, or BMP”
Example 4: Test a path of file/directory created/modified at a specific date
The NewerThan or OlderThan parameters can be used to test if a file is newer or older than a specific date. The example code is as follows to find if the PNG files were created during the specified date.
$testPath = Test-Path $testPath -NewerThan 'July 30, 2022'
Clear-Host
$testPath
The Test-Path also accepts the “-NewerThan” parameter to check the creation/modification of specific data.
As you can see that the file is older than July 30, 2022 therefore, the output is false.
Let’s see the second example with -OlderThan. Here is the example code.
$testPath = Test-Path $testPath -OlderThan 'July 30, 2023'
Clear-Host
$testPath
As you can see that the file is created before the specified date i.e., July 30, 2023.
The file is created before the mentioned date; therefore, the Test-Path cmdlet has returned true.
Bonus Tip
With the below command, you can explore the other useful functions of Test-Path.
Conclusion
In PowerShell, the Test-Path cmdlet is used to test the path of a directory/file. Test-Path has many other valuable functions, such as finding the date of creation of a file/folder, the type of the file/folder path, etc. In this article, we have explored the central theme of the test–path PowerShell cmdlet. For a better understanding, we have demonstrated the application of the Test-Path cmdlet alongside various parameters.