Learn Four Ways to Use PowerShell to Create Folders

Summary: Microsoft Scripting Guy, Ed Wilson, shows four ways to create folders with Windows PowerShell, and he discusses the merits of each approach.

Hey, Scripting Guy! Question Hey, Scripting Guy! I am trying to find the best way to create a new folder while using Windows PowerShell. I have seen many ways of creating folders in scripts that I have run across on the Internet. They all seem to use something different. Is there a best way to create a new folder?


Hey, Scripting Guy! AnswerHello GW,

Microsoft Scripting Guy, Ed Wilson, is here. This morning on Twitter, I saw a tweet that said “New-TimeSpan tells me that there are 41 days until the 2012 Scripting Games.” Indeed. Even though I have been busily working away on the Scripting Games, it seems awfully soon. I fired up Windows PowerShell and typed the following code:

New-TimeSpan -Start 2/21/12 -End 4/2/12

Sure enough, the returned TimeSpan object tells me that there are indeed 41 days until the 2012 Scripting Games. I decided I would like a cleaner output, so I used one of my Top Ten Favorite Windows PowerShell Tricks: group and dot. The revised code is shown here.

(New-TimeSpan -Start 2/21/12 -End 4/2/12).days

Both of these commands and the associated output are shown in the image that follows.

Image of command output

GW, you are correct, there are lots of ways to create directories, and I will show you four of them…

Method 1

It is possible to use the Directory .NET Framework class from the system.io namespace. To use the Directory class to create a new folder, use the CreateDirectory static method and supply a path that points to the location where the new folder is to reside. This technique is shown here.


When the command runs, it returns a DirectoryInfo class. The command and its associated output are shown in the image that follows.

Image of command output

I do not necessarily recommend this approach, but it is available. See the Why Use .NET Framework Classes from Within PowerShell Hey, Scripting Guy! blog for more information about when to use and not to use .NET Framework classes from within Windows PowerShell.

Method 2

Another way to create a folder is to use the Scripting.FileSystemObject object from within Windows PowerShell. This is the same object that VBScript and other scripting languages use to work with the file system. It is extremely fast, and relatively easy to use. After it is created, Scripting.FilesystemObject exposes a CreateFolder method. The CreateFolder method accepts a string that represents the path to create the folder. An object returns, which contains the path and other information about the newly created folder. An example of using this object is shown here.

$fso = new-object -ComObject scripting.filesystemobject


This command and its associated output are shown in the following image.

Image of command output

Method 3

GW, it is also possible to use native Windows PowerShell commands to create directories. There are actually two ways to do this in Windows PowerShell. The first way is to use the New-Item cmdlet. This technique is shown here.

New-Item -Path c:\test3 -ItemType directory

The command and the output from the command are shown here.

Image of command output

Compare the output from this command with the output from the previous .NET command. The output is identical because the New-Item cmdlet and the [system.io.directory]::CreateDirectory command return a DirectoryInfo object. It is possible to shorten the New-Item command a bit by leaving out the Path parameter name, and only supplying the path as a string with the ItemType. This revised command is shown here.

New-Item c:\test4 -ItemType directory

Some might complain that in the old-fashioned command interpreter, cmd, it was easier to create a directory because all they needed to type was md–and typing md is certainly easier than typing New-Item blah blah blah anyday.

Method 4

The previous complaint leads to the fourth way to create a directory (folder) by using Windows PowerShell. This is to use the md function. The thing that is a bit confusing, is that when you use Help on the md function, it returns Help from the New-Item cmdlet—and that is not entirely correct because md uses the New-Item cmdlet, but it is not an alias for the New-Item cmdlet. The advantage of using the md function is that it already knows you are going to create a directory; and therefore, you can leave off the ItemType parameter and the argument to that parameter. Here is an example of using the md function.

md c:\test5

The command and its associated output are shown here.

Image of command output

You can see from the image above that the md function also returns a DirectoryInfo object. To me, the md function is absolutely the easiest way to create a new folder in Windows PowerShell. Is it the best way to create a new folder? Well, it all depends on your criteria for best. For a discussion of THAT topic, refer to my Reusing PowerShell Code—What is Best blog.

GW, that is all there is to creating folders. Join me tomorrow for more Windows PowerShell coolness.

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 (11)

  1. Anonymous says:

    Excellent article, especially with explaining and giving examples of the different methods one can use within PowerShell. As always you are the best Ed.

  2. mredwilson says:

    @B-K that is a great tip … in fact, I was not aware of that issue. It does go back to the basic idea however … when you are writing a script, you should spell things out … it is always safest. Thank you … I imagine it was a bear of a thing to troubleshoot!

  3. K_Schulte says:

    Hi Ed,

    I would recommend and in fact I usually do use the "md <dir>" function most of the time. I didn't realize that this a function btw. … but do we really have to …?

    I'm still used to the way cmd.exe works and so I don't even think too much about the wonder of the "md" function in PS 🙂

    But still … it is great and remarkable that it returns an object to work with later on! That's a crucial difference between the cmd.exe-md and the ps-md version!

    Klaus (Schulte)

  4. ed wilson says:

    @K_Schulte no you do not HAVE to know that it is a function and not a cmdlet. But knowing that it is a function you are using, and NOT the intrinsic md command that exists in the cmd shell is important. The reason is that it helps to explain some of the differences you will find. Besides, personally, I like to know what I am doing. The more I know about what is actually going on under the covers of PowerShell the better I am able to unerstand some of the strangeness of PowerShell. The cool thing about PowerShell is that it is easy to learn, but nearly impossible to master.

  5. B-K says:

    Be careful using md/mkdir in scripts where Set-StrictMode is on… More details here:


  6. www.blueprototype.com says:

    Nice, this helped me alot- Espen

  7. Sirch says:

    Method 4 works best..Classic and old DOS habit..

    $xFolder = {md d:xtxt}

    Invoke-Command $xFolder

    Thanks TechNET! Rock ON!!!

  8. Kyle says:

    Hello Ed,

    How would you go about using the New-Item -ItemType Directory -Path with a unc path? I have tried this with full admin rights to the path i am trying to create a directory in.


  9. rdtechie says:

    Just for the info, md is a alias to mkdir, which in turn is a nice advanced function which invokes new-item. Would be nice to see a native cmdlet for this. Something like New-Folder, Set-Folder, Remove-Folder.

  10. Srinivas Kadiyala says:

    Hi Ed,

    When i tried:
    PS C:Userssrini_000> md D:S.K2014 BeyondMy CoursesPythonmystuff

    mkdir : A positional parameter cannot be found that accepts argument ‘BeyondMy’.
    At line:1 char:1
    + md D:S.K2014 BeyondMy CoursesPythonmystuff
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (:) [mkdir], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,mkdir

    But it worked, when
    PS C:Userssrini_000> md ‘D:S.K2014 BeyondMy CoursesPythonmystuff’

    Path is in quotes.

    Any reason ?

  11. Claeiyke says:

    Thanks for throwing that light in making a directory in windows powershell, how then can I change to the directory made in powershell?

Skip to main content