Powershell offers cmdlets and .NET class to check if a specific directory exists on the system. We will look at two different ways to check if the folder or path exists on a system.
Find if a directory exists using Test-Path in Powershell
Test-Path is a Powershell cmdlet that checks for the presence of a folder or directory on the system. The output of this command is boolean in nature. If the directory path is found, you will get the output as TRUE. If the directory path is not found, you will get the output as FALSE.
We will have a look at Test-Path in play here using an example. For the example, we will assign the folder path to a variable and check for the directory path using the Test-Path cmdlet.
$folder1=”d:\test_folder_1\”
Test-Path $folder1
In the commands shared above, we are creating a folder and then using Test-Path to get the confirmation.
The screenshot below represents both commands and the subsequent output. You can see that the output contains ‘True’. It confirms the presence of the directory or directory path on the system.
Now, we will look at a directory path that does not exist on the system and watch out for the output of the commands given below.
$folder2=”d:\test_folder_2\”
Test-Path $folder2
The commands and the consequent boolean output is displayed below. The screenshot shows that the output of the commands is False, and hence the directory path is not found on the system.
Generally speaking, the test-path command is used to capture the result and use it as part of an if statement or as part of a Powershell script.
One such Powershell script is pasted below:
if ((Test-Path “D:\test_folder_1) -eq $true) {
Write-Host “Directory Found”
}
else {
Write-Host “Directory not found”
}
Find if the directory exists using [System.IO.Directory] class
[System.IO.Directory] is an in-built class that can be used to check for the existence of a directory path on the system. We will use the same approach as we discussed above.
We will assign the directory path to the variable and check the variable through the [System.IO.Directory] .NET class. The commands that can be used to check if a directory exists on the system are shared below.
First we will define the variable as under:
$folder1=”d:\test_folder_1\”
Now, we will use the [System.IO.Directory] .NET class.
[System.IO.Directory]::Exists($folder1)
The output of the command the [System.IO.Directory] is shared in the screenshot given below here.
The output of the check for directory path through [System.IO.Directory] will be a boolean result in the form of a boolean value of ‘True’ or ‘False’.
You can integrate this method in a Powershell script and use it to trigger consequent actions.
Summary
We have seen two different methods by which we can check if a directory or folder path exists on a computer system using Powershell. Directory path checks are important as these can be used as part of if statements for triggering specific actions on a system.
Rajesh Dhawan is a technology professional who loves to write about Cyber-security events and stories, Cloud computing and Microsoft technologies. He loves to break complex problems into manageable chunks of meaningful information.