Recently, I was working with some files and folder operations using PowerShell. I was trying to check if a folder exists in PowerShell. There are different methods to achieve it.
To check if a folder exists in PowerShell, you can use the Test-Path cmdlet, which returns a Boolean value indicating the presence of the directory. For example, $folderExists = Test-Path -Path “C:\Example\Folder” will set $folderExists to $true if the folder exists, and $false if it does not. This cmdlet is the simplest and most direct method to verify the existence of a folder in PowerShell.
Check If a Folder Exists in PowerShell
Let us check out different methods to check if a folder exists in PowerShell.
1. Using Test-Path Cmdlet
The best way to check for the existence of a folder in PowerShell is by using the Test-Path cmdlet. This cmdlet is straightforward and returns a Boolean value indicating the presence of a file or directory.
Here is an example.
$folderPath = "C:\MyFolder\Folder-1"
$folderExists = Test-Path -Path $folderPath
Write-Host "Does the folder exist? $folderExists"If $folderExists is $true, the folder exists; if $false, it does not.
You can see the output in the screenshot below after I executed the PowerShell script using VS code. It displays true as the folder exists.

2. Using .NET Class Methods
PowerShell can call on the .NET Framework’s classes and methods. System.IO.Directory class has an Exists() method that can be used to check if a folder exists.
Here is an example:
$folderPath = "C:\MyFolder\Folder-2"
$folderExists = [System.IO.Directory]::Exists($folderPath)
Write-Host "Does the folder exist? $folderExists"This method is as effective as Test-Path, and is a matter of preference or specific use case.
3. Handling Errors and Exceptions
When checking for folders using PowerShell, it’s also important to handle potential errors properly. You can use try-catch blocks to manage exceptions that might occur, especially when dealing with permissions or inaccessible paths.
$folderPath = "C:\MyFolder"
try {
$folderExists = Test-Path -Path $folderPath
Write-Host "Does the folder exist? $folderExists"
} catch {
Write-Host "An error occurred: $_"
}In this example, if an error occurs while checking the folder’s existence, it will be caught, and a message will be displayed.
Conclusion
PowerShell offers several methods to check if a folder exists, like Test-Path cmdlet and .NET Framework methods.
In this PowerShell tutorial, I have explained how to check if a folder exists in PowerShell.
You may also like the following tutorials:
- Get folder size in GB in PowerShell
- How to Check if a Folder is Empty in PowerShell?
- How to Create folder if not exist in PowerShell?
- Delete Contents of Folder in PowerShell
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.