Learn about Windows PowerShell
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! 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?
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.
GW, you are correct, there are lots of ways to create directories, and I will show you four of them…
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.
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.
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.
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.
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.
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.
The command and its associated output are shown here.
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 firstname.lastname@example.org, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy
<p>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 ...?</p>
<p>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 :-) </p>
<p>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!</p>
<p>@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.</p>
<p>Be careful using md/mkdir in scripts where Set-StrictMode is on... More details here:</p>
<p><a rel="nofollow" target="_new" href="http://stackoverflow.com/questions/5021720/powershell-mkdir-alias-set-strictmode-version-2-strange-bug-why">stackoverflow.com/.../powershell-mkdir-alias-set-strictmode-version-2-strange-bug-why</a></p>
<p>@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!</p>
Nice, this helped me alot- Espen
Excellent article, especially with explaining and giving examples of the different methods one can use within PowerShell. As always you are the best Ed.