Weekend Scripter: Use PowerShell to Create Folder

Doctor Scripto

Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to create folders.

Microsoft Scripting Guy, Ed Wilson, is here. I am not a huge fan of using a mouse. In fact, the more things I can do from Windows PowerShell the better. It is a huge waste of time for me when I have to remove my hands from the keyboard, chase down a mouse, mouse around for a while, and then return my fingers to the home row on the keyboard. One cool thing is that Microsoft Word has a number of keyboard shortcuts—of course, it also has an API. I also do quite a bit of scripting for that.

Another place where I use Windows PowerShell quite a bit is for creating folders, directories, or containers (whatever we are calling them this week). I know that Windows creates lots of default folders, but they seem to be buried in my profile, and they are not all that accessible. I prefer to create my own directory structure to make it easier to copy, back up, and to use from within a Windows PowerShell script or console.

Something I often see is that scripters test for the existence of a folder, then if the folder does not exist, they create it. Here is a typical form of this code:

$path = "c:\fso"

If (Test-Path -Path $path -PathType Container)

    { Write-Host "$path already exists" -ForegroundColor Red}

    ELSE

        { New-Item -Path $path  -ItemType directory }

Although the previous code works, it is an awful lot of work. One might decide to add this as an ISE script snippet to simplify the coding process.

But what is the purpose of the code? Most of the time, the purpose is to avoid an error message that occurs when creating a folder that already exists. If this is the purpose of writing such code, you can avoid the error message by using the –Force parameter. Here is an example:

New-Item -Path c:\fso1  -ItemType directory -Force

In the following image, I run this command twice. Note that no error occurs.

Image of command output

I can use the New-Item cmdlet to create a nested folder—even if the root folders do not exist. This is shown here:

$path = "c:\fso3\fso3\fso3\fso3"

Remove-Item $path -Recurse -Force

New-Item -Path $path  -ItemType directory -Force

My favorite way to create a new folder is to use the MKDIR function (MD is an alias for MKDIR). MKDIR is cool because it already knows that I want to make a folder, and so I can skip that parameter. I can also specify an array of folder names in the function. I can specify the –Force parameter to keep the command from generating errors if a folder already exists. Here is an example:

$path = "C:\fso","C:\fso1","C:\fso2"

md $path -Force

The command and its output are shown here:

Image of command output

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy 

0 comments

Discussion is closed.

Feedback usabilla icon