Table of Contents
This tutorial will explain how to create a folder using PowerShell if it does not exist in the given location.
Using Test-Path
and New-Item
Cmdlets
To create folder if not exist in PowerShell:
- Use
Test-Path
cmdlet with if statement to check the path. - If folder doesn’t exist, Create a new folder with
New-Item
cmdlet.
1 2 3 4 5 6 7 8 9 10 |
$path = "C:\test\New Folder" if(Test-Path -Path $path){ Write-Host "Folder already exists." } else{ New-Item -Path $path -ItemType Directory Write-Host "Folder created successfully." } |
Output:
1 2 3 4 5 6 7 8 9 |
Directory: C:\test Mode LastWriteTime Length Name ---- ------------- ------ ---- d----- 22-12-2022 01:52 New Folder Folder created successfully. |
First, we assigned the folder path to the $path
variable. Then we used Test-Path
to determine whether the folder exists in that location. It checks whether all elements in the path exist.
If the path exists, the statement is True
, and the first command gets executed. It prints the message Folder already exists
.
If the path does not exist, New-Item
creates a directory New Folder
in the C:\test
directory. At last, the Write-Host
outputs the message Folder created successfully
.
New-Item
is cmdlet which is used to create new item. In this example, we created new folder with the help of New-Item
cmdlet.
Using Directory.Exists()
Method
To check if a folder exists in the given path, use [System.IO.Directory]::Exists()
method.
1 2 3 4 5 6 7 8 9 10 |
$path = "C:\test\New Folder2" if([System.IO.Directory]::Exists($path)){ Write-Host "Folder already exists." } else{ New-Item -Path $path -ItemType Directory Write-Host "Folder created successfully." } |
Output:
1 2 3 4 5 6 7 8 9 |
Directory: C:\test Mode LastWriteTime Length Name ---- ------------- ------ ---- d----- 22-12-2022 01:55 New Folder2 Folder created successfully. |
The .NET
class’s [System.IO.Directory]::Exists()
method is used as the conditional test in the if
statement. It evaluates to True
if the path exists and False
if it does not. If the return value is False
, New-Item
creates a new folder in the specified path.
Further reading:
Using Get-Item
with New-Item
Cmdlet
To create a folder if it does not exist:
- Use
Get-Item
to check the path. - Use
New-Item
to create a folder.
1 2 3 4 5 6 7 8 9 10 |
$path = "C:\test\New Folder3" if(Get-Item $path -ErrorAction Ignore){ Write-Host "Folder already exists." } else{ New-Item -Path $path -ItemType Directory Write-Host "Folder created successfully." } |
Output:
1 2 3 4 5 6 7 8 9 |
Directory: C:\test Mode LastWriteTime Length Name ---- ------------- ------ ---- d----- 22-12-2022 01:58 New Folder3 Folder created successfully. |
In the above example,
$path
contains the folder name and path.Get-Item
checks if the folder exists in the path.New-Item
creates a new folder if it does not exist in the given location.-ErrorAction Ignore
hides the exception raised byGet-Item
when the path is not found.
That’s it. Now you should know how to create folder if not exists in PowerShell. If you have any confusion, let us know in the comments.