Powershell

PowerShell Create Folder If Not Exists

Do you have a bunch of folders and want to create a new one, but don’t know whether that already folder exists or not? This process can take a lot of time because first, you will search for that folder, whether it exists or not, and then create it if it does not exist.

Surprisingly, PowerShell can save you a lot of time and effort if you know a little bit of scripting.

Hold on! You don’t need to learn the whole scripting thing for that task. Because I have provided five methods to create a folder if not exist with a proper explanation of every command used in it.

Quick Outline:

Before moving towards scripts let’s learn how to check whether a folder exists or not and then to a method to create a folder in PowerShell.

Check if the Folder Already Exists in PowerShell

The Test-Path command is used to check if the folder already exists in PowerShell. To check whether the folder exists, the folder path is specified to the Test-Path command, using the -Path parameter. In return, the Test-Path command returns True value if it exists, and False if it does not exist.

For instance, let’s check if the specified folder exists or not using the Test-Path command:

Test-Path -Path "C:\Document"

To check the existence of the folder, first, specify the Test-Path command and provide the folder path using the -Path parameter:

The output is False because the specified folder does not exist.

Create a New Folder in PowerShell

The New-Item command creates a new folder in PowerShell. To create a directory, provide the directory path to the New-Item command using the -Path parameter. Most importantly, use the -ItemType parameter to create a folder and specify the Directory value. If you don’t specify the -ItemType parameter and the Directory value, then the New-Item command will create a file instead of a folder.

For instance, let’s create a new folder using the New-Item command in PowerShell:

New-Item -Path "C:\Document" -ItemType Directory

To create a new folder:

  • First, use the New-Item command and specify the folder path to be created to the -Path parameter.
  • After that, specify the Directory value to the -ItemType parameter to tell the console to create a folder instead of a file:

The new folder has been created and its creation time is also visible in the above output.

Create a Folder If Not Exists in PowerShell

From the query, it can be observed that it is going to be an if-else scenario, which states that if the folder does not exist then create a new folder. There are five methods to create a folder if not exist in PowerShell, and each method involves the if-else condition scenario.

Quick Solution

Here is the quick solution to create a folder if not exist in the PowerShell console:

if (-not (Test-Path -Path "C:\NewFolder")) { New-Item -Path "C:\NewFolder" -ItemType Directory }

The specified directory does not exist and hence it is created.

1. Create Folder If Not Exists in PowerShell Using Test-Path

The Test-Path command checks the existence of the folder and tells the console if it does exist or not. The Test-Path and New-Item command along with the If-Else condition help create a folder if it does not exist in PowerShell.

For instance, let’s observe this code to create a folder if not exist using the Test-Path command:

if (Test-Path -Path "C:\NewFolder") {

Write-Host "Specified Folder Already Exists"
}
else
{
New-Item -Path "C:\NewFolder" -ItemType Directory
}

To create a folder using the Test-Path and New-Item command:

  • First, we created an If condition, where we used the Test-Path command and specified the folder using the -Path to check folders existence.
  • If the specified folder exists, then the message will be displayed using the Write-Host command stating that the folder already exists, and the script will end here.
  • Else the program will move to the next section.
  • In the else condition, we used the New-item command and specified the folder path to be created.
  • To more clearly explain the console that creates a folder, we used the -ItemType property and specified the value Directory:

Output: The folder does not exist and hence new folder is created in the specified directory.

2. Create Folder If Not Exists in PowerShell Using Get-Item

The Get-Item command gets the file or folder from the specified location. The Get-Item command can also check the existence of the folder using the if condition.

This is how you can create a folder if not exist in PowerShell using the Get-Item command:

if(Get-Item -Path "C:\Document" -ErrorAction Ignore){
Write-Host "File already exists"
}
else {
New-Item -Verbose "C:\Document" -ItemType Directory
}

To create a folder if it does not exist use the Get-Item command:

  • First, create an If condition, where use the Get-Item command and specify the folder path using the -Path parameter to check if it already exists or not.
  • Also, use the -ErrorAction parameter and specify the Ignore value to ignore errors that may occur during the code execution.
  • If the folder already exists the console will display the message saying that the folder already exists using the Write-Host command.
  • If the folder does not exist the program will move to the else condition where the new folder will be created.
  • In the else condition, use the New-Item command, specify the folder path to be created, and use the -ItemType parameter having the value Directory specified to it.
  • Alongside it, specify the -verbose parameter to get more detailed output:

The screenshot confirms that the specified folder does not exist and hence new folder was created.

3. Create Folder If Not Exists in PowerShell Using Get-ChildItem

The Get-ChildItem command gets the files or folders from the given directory. It can be used with the New-Item command and If-else condition to create a folder if not exist in PowerShell.

Here is the example code to create a folder if not exists with the aid of Get-ChildItem command:

if(Get-ChildItem -Path "C:\Document" -ErrorAction Ignore)
{
Write-Host "Folder Already Exists"
}
else
{
New-Item "C:\Document" -ItemType Directory
}

Note: The code explanation for the above snippet is the same as for the Get-Item command except for the Get-ChildItem command:

The specified folder does not exist and hence new folder was created.

4. Create a Folder If Not Exists in PowerShell Using Test-Path and Mkdir

The mkdir command creates a folder in PowerShell. It has an alias md, which also functions the same as the Mkdir command does. This command can also create a folder if not exist with the combination of the Test-Path command and if-else condition.

Here is the example of creating a folder if not exist in PowerShell using md and Test-Path command:

if (Test-Path -Path "C:\Folder") {

Write-Host "Folder Already Exists"
}
else
{
md "C:\Folder"
}

To create a folder if it does not exist:

  • First, create the If condition and specify the Test-Path command along with the folder to check whether it exists or not.
  • If the folder exists, end the code here and print the folder that already exists.
  • Else, create a folder using the md command by specifying the folder name and path:

The output shows that the folder does not exist and hence new folder was created.

5. Create Folder If Not Exists in PowerShell Using [System.IO.File]::Exists()

The [System.IO.File]::Exists() command checks whether the specified folder exists or not. It can create the folder in PowerShell if not exist with the combination of the if-else condition and New-Item command.

This is how you can create a folder if not exist in PowerShell using [System.IO.File]::Exists() command:

if([System.IO.Directory]::Exists("C:\Docs"))
{
Write-Host "Folder Already Exists"
}
else
{
New-Item "C:\Docs" -ItemType Directory
}

In the above code:

  • First, create an If condition and specify the [System.IO.Directory]::Exists(Folder-Path) command inside it to check if that folder exists or not.
  • If the directory is found, then the console will print that the folder exists.
  • Else, it will create that folder using the New-Item command:

The specified folder does not exist and then a new folder is created using the New-Item command.

Bonus Tip: Create a Folder if it Already Exists in PowerShell

The -Force parameter with the New-Item command is used to overwrite an existing folder in PowerShell. The -Force parameter overwrites an existing item in the specified path in PowerShell. To create a folder if it already exists, simply add the -Force parameter along with the New-Item command.

This is how you can overwrite an existing folder:

New-Item -Path "C:\Document" -ItemType Directory -Force

To create a folder if it already exists in PowerShell:

  • First, use the New-Item command and specify the folder to be created using the -path parameter.
  • Then, use the -ItemType parameter and specify the value to tell the console to create a folder.
  • Lastly, to overwrite an existing folder use the -Force parameter:

The folder is successfully overwritten in the specified path.

Conclusion

To create a folder if not exist, first, check the folder’s existence using the Test-Path command. Then, use the New-Item command to create a folder, if it does not exist. To perform this operation in one go then you need to use the if-else condition along with the Test-Path command and New-Item command, which I already demonstrated in Method 1 in this article.

About the author

Muhammad Farhan

I am a Computer Science graduate and now a technical writer who loves to provide the easiest solutions to the most difficult problems related to Windows, Linux, and Web designing. My love for Computer Science emerges every day because of its ease in our everyday life.