In this tutorial, I will explain how to create a folder if it does not exist in PowerShell.
Recently, I worked on a PowerShell script to automate deployments for a web application for a client headquartered in New York City. One requirement was to make sure a specific folder structure was in place on the server before copying over the latest build. However, I didn’t want the script to fail if the folders already existed from a previous run.
PowerShell Create Directory If Not Exists
After some research, I found out how to create a folder in PowerShell only if it doesn’t already exist by combining two cmdlets:
Here’s the basic syntax:
$path = "C:\Deployments\WebApp\Files"
if (!(Test-Path -PathType Container $path)) {
New-Item -ItemType Directory -Path $path
}
Let’s break this down:
- First I specify the
$pathvariable with the full directory path I want to create, likeC:\Deployments\WebApp\Files. - The
ifstatement usesTest-Pathwith the-PathType Containeroption to check if$pathexists as a directory. - By wrapping
Test-Pathin parentheses prefixed with!, it evaluates totrueif the path does not exist. - If true,
New-Itemis called with-ItemType Directoryto create the folder structure defined in$path
I executed the above PowerShell script using VS code, and you can see the exact output in the screenshot below.

Check out Create a File in PowerShell if it Doesn’t Exist
PowerShell Create Folder If Not Exist Example
Now, let me show you a very real example that will help you understand it better.
Let’s say I need to create this folder structure for my web application deployment:
C:\Deployments
└───WebApp
├───Backups
├───Files
└───Logs
Here’s how I would ensure those folders exist using PowerShell:
$baseDir = "C:\Deployments"
$appName = "WebApp"
$folders = "Backups", "Files", "Logs"
foreach ($folder in $folders) {
$path = Join-Path -Path $baseDir -ChildPath "$appName\$folder"
if (!(Test-Path -PathType Container $path)) {
New-Item -ItemType Directory -Path $path
}
}
This script:
- Defines variables for the base directory
$baseDir, application name$appName, and an array of subfolders in$folders - Loops through each
$folderin$folders:- Constructs the full
$pathby joining$baseDir,$appName, and the current$folder - Checks if the
$pathexists usingTest-Path - If it doesn’t exist, creates it with
New-Item
- Constructs the full
By using a loop, it keeps the code concise and easy to modify if I need to add or remove folders later.
The exact output is in the screenshot below; you can see it created two new folders that did not exist.

Read Create a Log File using PowerShell
Handle Errors While Creating Folder If Not Exist
It is very important to handle errors while creating folders using PowerShell if it does not exist. The error can be due to anything, such as insufficient permissions or an invalid path name.
Here is the complete script where I have added try/catch to handle errors.
$baseDir = "C:\Deployments"
$appName = "WebApp"
$folders = "Backups", "Files", "Logs"
foreach ($folder in $folders) {
$path = Join-Path -Path $baseDir -ChildPath "$appName\$folder"
if (!(Test-Path -PathType Container $path)) {
try {
New-Item -ItemType Directory -Path $path -ErrorAction Stop
Write-Host "Created directory: $path"
}
catch {
Write-Error "Failed to create directory '$path': $($_.Exception.Message)"
}
}
}
Now if New-Item encounters an error, it will:
- Output an error message with
Write-Errorincluding the path and exception details - Continue on to the next folder instead of halting the script
Check out PowerShell Copy-Item with Folder Structure
Create Folder If Not Exist in PowerShell with -Force Option
To make the folder creation code even more concise, I can use the -Force option with New-Item. This tells it to create any missing parent directories in the path if needed.
Instead of checking if the path exists first, I can just do:
$baseDir = "C:\Deployments"
$appName = "WebApp"
$folders = "Backups", "Files", "Logs"
foreach ($folder in $folders) {
$path = Join-Path -Path $baseDir -ChildPath "$appName\$folder"
New-Item -ItemType Directory -Path $path -Force
}
With -Force, the script will create the specified $path regardless if it already exists, without throwing an error. This eliminates the need for the if statement and Test-Path. However, it’s a little less explicit about the intent to only create folders if missing.
Conclusion
In this tutorial, we covered how to create a folder in PowerShell only if it doesn’t already exist by using:
Test-Path -PathType Containerto check if a directory existsNew-Item -ItemType Directoryto create a folder-Forceoption to create missing parent directories and ignore existing ones- Error handling with
try/catchto gracefully handle failure
I showed a real-world example of creating a directory structure for deploying a web application, with variables and looping. If you still have any questions, feel free to leave a comment below.
You may also like:
- PowerShell Copy-Item Cmdlet to Copy Files and Folders
- Get File Size Using PowerShell
- Get the Last Modified Date of a File in PowerShell

Hey! I’m Bijay Kumar, founder of SPGuides.com and a Microsoft Business Applications MVP (Power Automate, Power Apps). I launched this site in 2020 because I truly enjoy working with SharePoint, Power Platform, and SharePoint Framework (SPFx), and wanted to share that passion through step-by-step tutorials, guides, and training videos. My mission is to help you learn these technologies so you can utilize SharePoint, enhance productivity, and potentially build business solutions along the way.
Thank you for this.
I would suggest one change by adding an additional variable for the -Path so the script can be quickly used anywhere with just changing the variables at the top.
$folderName = (Get-Date).tostring(“dd-MM-yyyy”)
$root=”E:Desktop”
$path=$Root+$folderName
if (!(Test-Path $Path))
{
New-Item -itemType Directory -Path $root -Name $FolderName
}
else
{
write-host “Folder already exists”
}