Create a Folder If Not Exists in PowerShell

We can check if a folder exist or not by using the PowerShell cmdlet Test-Path and create a new folder using New-Item cmdlet.

The below powershell script will check whether the folder “Test” is already exists or not under the path C:Share and it creates new folder if it not exists already.

$dir = "C:\ShareTest"
if(!(Test-Path -Path $dir )){
    New-Item -ItemType directory -Path $dir
    Write-Host "New folder created"
}
else
{
  Write-Host "Folder already exists"
}

Advertisement

Leave a Comment