Menu & Search

PowerShell Create Folder If Not Exists

PowerShell Create Folder If Not Exists

In this post, I share a script that will help you create folders and sub-folders with PowerShell if they do not already exist. We use Test-Path in our PS scripts to check if objects exist before executing the create command.

This is one small part of a more detailed blog post I have on creating files and folders using PowerShell. My other post, How to Create New Files & Folders in PowerShell also covers more details on the differences between creating files vs creating folders.

Create New Folder (if not exists) in PowerShell

We’re creating a new folder, only if it doesn’t already exist – “C:\temp\demo”

The create folder command is New-Item, which runs conditionally depending on the Test-Path true/false result. When we run this script it also creates your C:\temp directory if it doesn’t already exist.

# create folder if not exists .ps1
$path = "c:\temp\demo"
If(!(Test-Path $path) ){
    New-Item -ItemType Directory -Force -Path $path
}

0 Comments