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
$path
variable with the full directory path I want to create, likeC:\Deployments\WebApp\Files
. - The
if
statement usesTest-Path
with the-PathType Container
option to check if$path
exists as a directory. - By wrapping
Test-Path
in parentheses prefixed with!
, it evaluates totrue
if the path does not exist. - If true,
New-Item
is called with-ItemType Directory
to 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
$folder
in$folders
:- Constructs the full
$path
by joining$baseDir
,$appName
, and the current$folder
- Checks if the
$path
exists 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-Error
including 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 Container
to check if a directory existsNew-Item -ItemType Directory
to create a folder-Force
option to create missing parent directories and ignore existing ones- Error handling with
try/catch
to 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
I am Bijay a Microsoft MVP (10 times – My MVP Profile) in SharePoint and have more than 17 years of expertise in SharePoint Online Office 365, SharePoint subscription edition, and SharePoint 2019/2016/2013. Currently working in my own venture TSInfo Technologies a SharePoint development, consulting, and training company. I also run the popular SharePoint website EnjoySharePoint.com
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”
}