≡ Menu

Check and create a folder if it doesn’t exists using PowerShell

There are several ways available with PowerShell to check and create a new folder at given location using. Combination of Test-Path and New-Item can do this task for you.

However, there is much more easy way available, if you are not bothered to check if the path exists and all you need is a folder there. The [System.IO.Directory] class has a method called CreateDirectory which just a creates a directory if it doesn’t exists. It will not do anything if the folder is already available.

Let us try this now.

[Void][System.IO.Directory]::CreateDirectory("C:\temp")            

Irrespecitve of how many no. of times you are executing the above command, it will never error out. It create the folder for the first time and doesn’t do anything during the subsequent runs because the folder is already avialable.

Hope you like this tip.