{"id":7411,"date":"2015-03-08T00:01:00","date_gmt":"2015-03-08T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2015\/03\/08\/weekend-scripter-use-powershell-to-create-folder\/"},"modified":"2019-02-18T10:30:22","modified_gmt":"2019-02-18T17:30:22","slug":"weekend-scripter-use-powershell-to-create-folder","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/weekend-scripter-use-powershell-to-create-folder\/","title":{"rendered":"Weekend Scripter: Use PowerShell to Create Folder"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to create folders.<\/span><\/p>\n<p>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&mdash;of course, it also has an API. I also do quite a bit of scripting for that.<\/p>\n<p>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.<\/p>\n<p>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:<\/p>\n<p style=\"margin-left:30px\">$path = &quot;c:\\fso&quot;<\/p>\n<p style=\"margin-left:30px\">If (Test-Path -Path $path -PathType Container)<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; { Write-Host &quot;$path already exists&quot; -ForegroundColor Red}<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; ELSE<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { New-Item -Path $path&nbsp; -ItemType directory }<\/p>\n<p>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.<\/p>\n<p>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 <b>&ndash;Force<\/b> parameter. Here is an example:<\/p>\n<p style=\"margin-left:30px\">New-Item -Path c:\\fso1&nbsp; -ItemType directory -Force<\/p>\n<p>In the following image, I run this command twice. Note that no error occurs.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-3-8-15-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-3-8-15-01.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>I can use the <b>New-Item<\/b> cmdlet to create a nested folder&mdash;even if the root folders do not exist. This is shown here:<\/p>\n<p style=\"margin-left:30px\">$path = &quot;c:\\fso3\\fso3\\fso3\\fso3&quot;<\/p>\n<p style=\"margin-left:30px\">Remove-Item $path -Recurse -Force<\/p>\n<p style=\"margin-left:30px\">New-Item -Path $path&nbsp; -ItemType directory -Force<\/p>\n<p>My favorite way to create a new folder is to use the <b>MKDIR<\/b> function (<b>MD <\/b>is an alias for <b>MKDIR<\/b>). 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 <b>&ndash;Force<\/b> parameter to keep the command from generating errors if a folder already exists. Here is an example:<\/p>\n<p style=\"margin-left:30px\">$path = &quot;C:\\fso&quot;,&quot;C:\\fso1&quot;,&quot;C:\\fso2&quot;<\/p>\n<p style=\"margin-left:30px\">md $path -Force<\/p>\n<p>The command and its output are shown here:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-3-8-15-02.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-3-8-15-02.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/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><span style=\"font-size:12px\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&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,4,12,61,45],"class_list":["post-7411","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-folders","tag-scripting-guy","tag-scripting-techniques","tag-storage","tag-weekend-scripter","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/7411","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=7411"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/7411\/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=7411"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=7411"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=7411"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}