{"id":11081,"date":"2012-02-21T00:01:00","date_gmt":"2012-02-21T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2012\/02\/21\/learn-four-ways-to-use-powershell-to-create-folders\/"},"modified":"2012-02-21T00:01:00","modified_gmt":"2012-02-21T00:01:00","slug":"learn-four-ways-to-use-powershell-to-create-folders","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/learn-four-ways-to-use-powershell-to-create-folders\/","title":{"rendered":"Learn Four Ways to Use PowerShell to Create Folders"},"content":{"rendered":"<p><b>Summary<\/b>: Microsoft Scripting Guy, Ed Wilson, shows four ways to create folders with Windows PowerShell, and he discusses the merits of each approach.<\/p>\n<p><span><span><span><span><span><span><span><span><img decoding=\"async\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\" \/><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span>&nbsp;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?<\/p>\n<p>&mdash;GW<\/p>\n<p><span><span><span><span><span><span><span><span><img decoding=\"async\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\" \/><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span>Hello GW,<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. This morning on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a>, I saw a tweet that said &ldquo;New-TimeSpan tells me that there are 41 days until the 2012 Scripting Games.&rdquo; 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:<\/p>\n<p style=\"padding-left: 30px\">New-TimeSpan -Start 2\/21\/12 -End 4\/2\/12<\/p>\n<p>Sure enough, the returned <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.timespan.aspx\" target=\"_blank\">TimeSpan object<\/a> tells me that there are indeed 41 days until the <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2012\/02\/04\/the-2012-windows-powershell-scripting-games-all-links-on-one-page.aspx\" target=\"_blank\">2012 Scripting Games<\/a>. I decided I would like a cleaner output, so I used one of my <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2011\/06\/18\/top-ten-favorite-powershell-tricks-part-1.aspx\" target=\"_blank\">Top Ten Favorite Windows PowerShell Tricks<\/a>: <i>group and dot. <\/i>The revised code is shown here.<\/p>\n<p style=\"padding-left: 30px\">(New-TimeSpan -Start 2\/21\/12 -End 4\/2\/12).days<\/p>\n<p>Both of these commands and the associated output are shown in the image that follows.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4034.hsg-2-21-12-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4034.hsg-2-21-12-01.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>GW, you are correct, there are lots of ways to create directories, and I will show you four of them&hellip;<\/p>\n<h3><b>Method 1<\/b><\/h3>\n<p>It is possible to use the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.io.directory.aspx\" target=\"_blank\">Directory<i> <\/i>.NET Framework class<\/a> from the <i>system.io <\/i>namespace. To use the <b>Directory<\/b><i> <\/i>class to create a new folder, use the <b>CreateDirectory<\/b><i> <\/i>static method and supply a path that points to the location where the new folder is to reside. This technique is shown here.<\/p>\n<p style=\"padding-left: 30px\">[system.io.directory]::CreateDirectory(&#8220;C:\\test&#8221;)<\/p>\n<p>When the command runs, it returns a <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.io.directoryinfo.aspx\" target=\"_blank\">DirectoryInfo<i> <\/i>class<\/a>. The command and its associated output are shown in the image that follows.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1803.hsg-2-21-12-02.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1803.hsg-2-21-12-02.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>I do not necessarily recommend this approach, but it is available. See the <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2012\/02\/11\/why-use-net-framework-classes-from-within-powershell.aspx\" target=\"_blank\">Why Use .NET Framework Classes from Within PowerShell<\/a><i> <\/i>Hey, Scripting Guy! blog for more information about <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2012\/02\/12\/use-net-framework-classes-to-augment-powershell-when-required.aspx\" target=\"_blank\">when to use<\/a> and not to use .NET Framework classes from within Windows PowerShell.<\/p>\n<h3><b>Method 2<\/b><\/h3>\n<p>Another way to create a folder is to use the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/z9ty6h50(v=VS.85).aspx\" target=\"_blank\">Scripting.FileSystemObject object<\/a> 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, <b>Scripting.FilesystemObject<\/b> exposes a <b>CreateFolder<\/b><i> <\/i>method. The <b>CreateFolder<\/b><i> <\/i>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.<\/p>\n<p style=\"padding-left: 30px\">$fso = new-object -ComObject scripting.filesystemobject<\/p>\n<p style=\"padding-left: 30px\">$fso.CreateFolder(&#8220;C:\\test1&#8221;)<\/p>\n<p>This command and its associated output are shown in the following image.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1004.hsg-2-21-12-03.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1004.hsg-2-21-12-03.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<h3><b>Method 3<\/b><\/h3>\n<p>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 <b>New-Item <\/b>cmdlet. This technique is shown here.<\/p>\n<p style=\"padding-left: 30px\">New-Item -Path c:\\test3 -ItemType directory<\/p>\n<p>The command and the output from the command are shown here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0820.hsg-2-21-12-04.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0820.hsg-2-21-12-04.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>Compare the output from this command with the output from the previous .NET command. The output is identical because the <b>New-Item <\/b>cmdlet and the [system.io.directory]::CreateDirectory command return a <b>DirectoryInfo<\/b><i> <\/i>object. It is possible to shorten the <b>New-Item <\/b>command a bit by leaving out the <i>Path <\/i>parameter name, and only supplying the path as a string with the <i>ItemType<\/i>. This revised command is shown here.<\/p>\n<p style=\"padding-left: 30px\">New-Item c:\\test4 -ItemType directory<\/p>\n<p>Some might complain that in the old-fashioned command interpreter, <b>cmd<\/b>, it was easier to create a directory because all they needed to type was <b>md<\/b><i>&ndash;<\/i>&ndash;and typing <b>md<\/b> is certainly easier than typing <b>New-Item <\/b>blah blah blah anyday.<\/p>\n<h3><b>Method 4<\/b><\/h3>\n<p>The previous complaint leads to the fourth way to create a directory (folder) by using Windows PowerShell. This is to use the <b>md<\/b><i> <\/i>function. The thing that is a bit confusing, is that when you use Help on the <b>md<\/b><i> <\/i>function, it returns Help from the <b>New-Item<\/b> cmdlet&mdash;and that is not entirely correct because <b>md<\/b><i> <\/i>uses the <b>New-Item <\/b>cmdlet, but it is not an alias for the <b>New-Item <\/b>cmdlet. The advantage of using the <b>md<\/b><i> <\/i>function is that it already knows you are going to create a directory; and therefore, you can leave off the <i>ItemType <\/i>parameter and the argument to that parameter. Here is an example of using the <b>md<\/b><i> <\/i>function.<\/p>\n<p style=\"padding-left: 30px\">md c:\\test5<\/p>\n<p>The command and its associated output are shown here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6888.hsg-2-21-12-05.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6888.hsg-2-21-12-05.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>You can see from the image above that the <b>md<\/b><i> <\/i>function also returns a <b>DirectoryInfo<\/b><i> <\/i>object. To me, the <b>md<\/b><i> <\/i>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 <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2011\/04\/30\/reusing-powershell-code-what-is-best.aspx\" target=\"_blank\">Reusing PowerShell Code&mdash;What is Best<\/a><i> <\/i>blog.<\/p>\n<p>GW, that is all there is to creating folders. Join me tomorrow for more Windows PowerShell coolness.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, shows four ways to create folders with Windows PowerShell, and he discusses the merits of each approach. &nbsp;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 [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[11,3,12,45],"class_list":["post-11081","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-folders","tag-scripting-guy","tag-storage","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, shows four ways to create folders with Windows PowerShell, and he discusses the merits of each approach. &nbsp;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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/11081","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=11081"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/11081\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=11081"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=11081"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=11081"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}