This is a very common requirement; you will often need to create a folder if it doesn’t exist in PowerShell. PowerShell provides a simple and effective way to check if a folder exists and create it if it doesn’t. In this tutorial, we will explore different methods for creating a folder if it doesn’t exist using PowerShell.
To create a folder in PowerShell if it doesn’t already exist, you can use the Test-Path cmdlet to check for the folder’s presence and then New-Item to create it. Here’s a concise example: $folderPath = ‘C:\MyFolder’; if (-not (Test-Path $folderPath)) { New-Item -Path $folderPath -ItemType Directory }. This script first checks if the path C:\MyFolder exists and, if not, creates the folder at that location.
Method 1: Using Test-Path and New-Item
The easiest way to create a folder only if it does not already exist in PowerShell is to use the Test-Path cmdlet to check for the folder’s existence and then New-Item to create the directory.
Here’s a complete PowerShell script that will first check if the folder exists, and if it does not exist, then it will create the folder.
$folderPath = "C:\MyNewFolder"
if (-not (Test-Path -Path $folderPath)) {
New-Item -Path $folderPath -ItemType Directory
Write-Output "Folder Created Successfully!"
}
else {
Write-Output "Folder already exists!"
}In this script, Test-Path checks whether the path exists. If it returns $false, the New-Item cmdlet executes, creating a new directory at the specified path.
You can see the output in the screenshot below after I executed the PowerShell script using VS code.

Method 2: Using New-Item with -Force Parameter
Another method to ensure a directory is created if it doesn’t exist is to use the -Force parameter with the New-Item PowerShell cmdlet. This approach is less common for directory creation since it doesn’t explicitly check for the existence of the directory. However, it can be useful in certain scenarios:
$folderPath = "C:\MyNewFolder"
New-Item -Path $folderPath -ItemType Directory -ForceThe -Force parameter will create the directory if it doesn’t exist, but it won’t overwrite an existing directory. Instead, it will ensure that the command doesn’t throw an error if the directory already exists.
Method 3: Using .NET Class [System.IO.Directory]::Exists()
You can also use the .NET classes in PowerShell scripts; you can use the [System.IO.Directory]::Exists() method to check if a folder exists:
$folderPath = "C:\MyNewFolder"
if (-not [System.IO.Directory]::Exists($folderPath)) {
[System.IO.Directory]::CreateDirectory($folderPath)
}This method is similar to the first method but uses .NET classes directly instead of PowerShell cmdlets.
Method 4: Combine Test-Path with a Function
Here is a function that I created, and you can reuse it. When you execute the function, it will first check if the folder exists, and if it doesn’t, it will create it.
function Create-DirectoryIfNotExist {
param (
[string]$Path
)
if (-not (Test-Path -Path $Path)) {
New-Item -Path $Path -ItemType Directory
}
}
# Usage
Create-DirectoryIfNotExist -Path "C:\MyNewFolder"This function, Create-DirectoryIfNotExist, takes a path as a parameter and creates the directory if it doesn’t exist. You can then call this function multiple times throughout your script.
Method 5: Using Try and Catch with Error Handling
If you want to handle errors when creating directories using PowerShell, such as when the path is invalid, or you don’t have the necessary permissions, you can use a try and catch block:
$folderPath = "C:\MyNewFolder"
try {
New-Item -Path $folderPath -ItemType Directory -ErrorAction Stop
} catch {
Write-Error "An error occurred while attempting to create the directory: $_"
}By setting -ErrorAction Stop, you’re instructing PowerShell to stop executing and go to the catch block if an error occurs. This allows you to handle the error gracefully.
Conclusion
PowerShell provides various ways to create a directory if it doesn’t exist like Test-Path and New-Item cmdlets. In this blog post, we’ve covered several methods to create folders if they don’t exist using PowerShell.
You may also like:
- Delete Contents of Folder in PowerShell
- Count Files in a Folder Using PowerShell
- PowerShell Foreach File in Folder
- Get folder size in GB in PowerShell
- How to Check if a Folder is Empty 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.