Weekend Scripter: Use PowerShell to Create Folder


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 

Comments (5)

  1. jrv says:

    Get and create hierarchy of folders in one step:

    $folder=New-Item c:testtesttesttesttest -force

    No need to test-path.

  2. Keith Garner says:

    Thanks for the article. I didn’t know that MkDir was a Script function not an alias. To see for yourself run the command:
    get-command mkdir | Select-Object -ExpandProperty definition

    Additionally, when I make directories in my scripts I will also pipe the results to "Out-Null" to prevent the directory name from polluting the output pipeline.

    $path = "C:fso","C:fso1","C:fso2"
    New-Item -Path $path -ItemType directory -Force | out-null

    Thanks again.

  3. Anonymous says:

    Thanks for the tip. I did not know the ‘-force’ parameter can be used instead of ‘test-path’ to suppress the errors.

  4. Brian says:

    Hey Ed,

    You must have some serious keyboarding skills to be able to create a folder (directory, container, whatever) faster via PoweShell than via mouse. Of course, if you’re already working in the directory within which you want to create the folder then "md " is
    probably faster. But other than that, I don’t know. To grab the mouse, open Explorer, navigate to wherever, and right click to create a folder is pretty darn quick. Plus you might be able to argue that it’s not a bad idea to take your hands off the keyboard
    once in a while. But maybe I’m just lame 😉

  5. josefismael says:

    ni
    type: directory

    Done

Skip to main content