How to Create a Folder If Not Exists in PowerShell?

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:

  1. Test-Path – Checks if a path exists
  2. New-Item – Creates a new directory

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:

  1. First I specify the $path variable with the full directory path I want to create, like C:\Deployments\WebApp\Files.
  2. The if statement uses Test-Path with the -PathType Container option to check if $path exists as a directory.
  3. By wrapping Test-Path in parentheses prefixed with !, it evaluates to true if the path does not exist.
  4. 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.

powershell create folder if not exists

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:

  1. Defines variables for the base directory $baseDir, application name $appName, and an array of subfolders in $folders
  2. Loops through each $folder in $folders:
    • Constructs the full $path by joining $baseDir$appName, and the current $folder
    • Checks if the $path exists using Test-Path
    • If it doesn’t exist, creates it with New-Item

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.

powershell create directory if not exists

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 exists
  • New-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:

  • 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”
    }

  • >
    Download User registration canvas app

    DOWNLOAD USER REGISTRATION POWER APPS CANVAS APP

    Download a fully functional Power Apps Canvas App (with Power Automate): User Registration App

    Power Platform Tutorial

    FREE Power Platform Tutorial PDF

    Download 120 Page FREE PDF on Microsoft Power Platform Tutorial. Learn Now…