{"id":17101,"date":"2010-09-16T00:01:00","date_gmt":"2010-09-16T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2010\/09\/16\/how-to-use-powershell-to-create-shared-folders-in-windows-7\/"},"modified":"2010-09-16T00:01:00","modified_gmt":"2010-09-16T00:01:00","slug":"how-to-use-powershell-to-create-shared-folders-in-windows-7","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-to-use-powershell-to-create-shared-folders-in-windows-7\/","title":{"rendered":"How to Use PowerShell to Create Shared Folders in Windows 7"},"content":{"rendered":"<p>&#160;<\/p>\n<p><b>Summary<\/b>: Guest blogger Sean Kearney shows you how to use Windows PowerShell to create shared folders and set permissions on a Windows desktop.<\/p>\n<p>&#160;<\/p>\n<p><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\" \/><\/p>\n<p> Hey, Scripting Guy! How can I use Windows PowerShell to create shares?<\/p>\n<p>&#8212; SK<\/p>\n<p>&#160;<\/p>\n<p><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\" \/>Hello SK, <\/p>\n<p>Microsoft Scripting Guy Ed Wilson here. Wrapping up Guest Blogger Week is Sean Kearney. Sean is a network administrator, a Microsoft Certified Technology Specialist in Windows Server Virtualization, Configuration, and a Microsoft Certified Systems Engineer and MVP in Windows PowerShell. He is a devoted and passionate computer enthusiast from the early 80s to the present day, having used just about every microcomputer ever. He is self-taught in computer programming with 65xx machine code, working with many technologies, but primarily Microsoft technologies. He deals with \u201canything thrown at him\u201d from gnawed keyboards to recovery of Exchange servers to networking setups and isolating the realm of the unknown. His present position has him testing and deploying just about any new Microsoft technology he\u2019s asked to as well as dealing with users in an enterprise class environment. Prior to this, he spent more than 8 years dealing with small business systems and home user environments. He absolutely loves Windows PowerShell, Windows 7, and Hyper-V and in that specific order. You will often find him hanging out online at <a href=\"http:\/\/www.powershell.ca\/\">http:\/\/www.powershell.ca<\/a>.<\/p>\n<p>Here\u2019s Sean.<\/p>\n<p>&#160;<\/p>\n<p>Creating networks shares is not a challenge.&#160;&#160; Fire up the GUI, right-click <strong>Share<\/strong>, add the user or group, and set the permissions on the share. This is shown in the following image. It is not difficult, but it is also not seamless.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7532.HSG09161001_272276F2.jpg\"><img decoding=\"async\" style=\"border-bottom: 0px;border-left: 0px;padding-left: 0px;padding-right: 0px;border-top: 0px;border-right: 0px;padding-top: 0px\" title=\"Image of creating network shares\" border=\"0\" alt=\"Image of creating network shares\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0523.HSG09161001_thumb_398AC7A7.jpg\" width=\"604\" height=\"444\" \/><\/a><\/p>\n<p>So why not make it that way? We can certainly do this thanks to free tools such as Windows PowerShell and VBScript. But why Windows PowerShell? One word. Interactivity. We can test and put the pieces together one bit at a time.<\/p>\n<p>Normally for a network share, we need something to share. A folder. So let\u2019s make one:<b><i><\/i><\/b><\/p>\n<blockquote>\n<div class=\"code\"><font color=\"#0000ff\">NEW-ITEM<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">C:\\NetworkShare<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">-type<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">directory<\/font> <\/div>\n<\/blockquote>\n<p>And then we\u2019re going to just\u2026oh wait! What if the directory is already there? We should probably check for that first. In Windows PowerShell, there is a simple cmdlet called <strong>Test-Path<\/strong> that does exactly that. It tests to see if a particular file or directory is actually there.&#160;&#160; It returns a Boolean <strong>$TRUE<\/strong> or <strong>$FALSE<\/strong>, depending on the results.<\/p>\n<p>So if I\u2019m going to make a new folder for the share, I can actually verify it\u2019s there first. But of course I want to create the folder if it\u2019s <em>not<\/em> there, so I plug in a \u201c!\u201d (NOT) into the decision<\/p>\n<p><b><i><\/i><\/b><\/p>\n<blockquote>\n<div class=\"code\"><font color=\"#0000ff\">IF<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">(!(<\/font><font color=\"#0000ff\">TEST-PATH<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">C:\\NewShare))<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">{<\/font><font color=\"#808080\">       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <\/font><font color=\"#0000ff\">NEW-ITEM<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">C:\\NewShare<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">-type<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">Directory<\/font><font color=\"#808080\">       <br \/><\/font><font color=\"#000000\">}<\/font> <\/div>\n<\/blockquote>\n<p>We now have folder and we have made sure to not mess up one that was already there. Now the tricky part: sharing. There is no cmdlet in Windows PowerShell for sharing, but there is the <strong>WIN32_Share<\/strong> class in WMI. To access it, all we need do is assign it to a variable for easy use:<\/p>\n<p><b><i><\/i><\/b><\/p>\n<blockquote>\n<div class=\"code\"><font color=\"#2b91af\">$Shares<\/font><font color=\"#0000ff\">=<\/font><font color=\"#000000\">[WMICLASS]\u201dWIN32_Share\u201d<\/font> <\/div>\n<\/blockquote>\n<p>To create a share, we call up the <strong>Create<\/strong> method from <strong>Win32_Share<\/strong>. We can find out which methods are available on this particular object by using our good old friend, <strong>GET-MEMBER<\/strong>:<\/p>\n<blockquote>\n<div class=\"code\"><font color=\"#2b91af\">$Shares<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">|<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#0000ff\">GET-MEMBER<\/font> <\/div>\n<\/blockquote>\n<p>You\u2019ll see right at the top a method called <strong>Create<\/strong>, which is used to create the shares. But <strong>Create<\/strong> has two rules:<\/p>\n<ol>\n<li>You must be an administrator on the machine.<\/li>\n<li>You must <strong>Run as administrator <\/strong>the Windows PowerShell console. <\/li>\n<\/ol>\n<p>So creating a share requires three parameters: the name of the folder you are sharing, the name of the share, and the type of the share.&#160; Don\u2019t forget we could be sharing something other than a drive potentially.<\/p>\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td valign=\"top\" width=\"48%\">\n<p><strong>Value<\/strong><\/p>\n<\/td>\n<td valign=\"top\" width=\"51%\">\n<p><strong>Meaning<\/strong><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"48%\">\n<p>0 (0x0)<\/p>\n<\/td>\n<td valign=\"top\" width=\"51%\">\n<p>Disk Drive<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"48%\">\n<p>1 (0x1)<\/p>\n<\/td>\n<td valign=\"top\" width=\"51%\">\n<p>Print Queue<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"48%\">\n<p>2 (0x2)<\/p>\n<\/td>\n<td valign=\"top\" width=\"51%\">\n<p>Device<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"48%\">\n<p>3 (0x3)<\/p>\n<\/td>\n<td valign=\"top\" width=\"51%\">\n<p>IPC<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"48%\">\n<p>2147483648 (0x80000000)<\/p>\n<\/td>\n<td valign=\"top\" width=\"51%\">\n<p>Disk Drive Admin<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"48%\">\n<p>2147483649 (0x80000001)<\/p>\n<\/td>\n<td valign=\"top\" width=\"51%\">\n<p>Print Queue Admin<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"48%\">\n<p>2147483650 (0x80000002)<\/p>\n<\/td>\n<td valign=\"top\" width=\"51%\">\n<p>Device Admin<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"48%\">\n<p>2147483651 (0x80000003)<\/p>\n<\/td>\n<td valign=\"top\" width=\"51%\">\n<p>IPC Admin<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>So with a folder called C:\\NetworkShare, if we try to share it as \u201cJoey\u201d we run the following command:<\/p>\n<p><b><i><\/i><\/b><\/p>\n<blockquote>\n<div class=\"code\"><font color=\"#2b91af\">$Shares<\/font><font color=\"#000000\">.Create(\u201cC:\\NetworkShare\u201d,\u201dJoey\u201d,<\/font><font color=\"#800000\">0<\/font><font color=\"#000000\">)<\/font> <\/div>\n<\/blockquote>\n<p>Yeah! It\u2019s done! Celebrate and\u2026oh, right. We should have checked to see if it was there first. Right. Would have been a good idea. Well, that\u2019s not a problem. That\u2019s a feature of Windows PowerShell and WMI. <font color=\"#000000\">Just<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">use<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">a<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#0000ff\">GET-WMIOBJECT<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">on<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\"><strong>Win32_share<\/strong>:<\/font><\/p>\n<blockquote>\n<p><font color=\"#0000ff\">GET-WMIOBJECT<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">Win32_Share<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">-filter<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">\u201cname<\/font><font color=\"#0000ff\">=<\/font><font color=\"#000000\">\u2019Joey\u2019\u201d<\/font> <\/p>\n<\/blockquote>\n<p>But like when we created the folder, we want this to happen only if it\u2019s not there.&#160; So we pull out that NOT (\u201c!\u201d) character:<\/p>\n<p><b><i><\/i><\/b><\/p>\n<blockquote>\n<div class=\"code\"><font color=\"#0000ff\">If<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">(!(<\/font><font color=\"#0000ff\">GET-WMIOBJECT<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">Win32_Share<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">-filter<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">\u201cname<\/font><font color=\"#0000ff\">=<\/font><font color=\"#000000\">\u2019Joey\u2019\u201d)<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">{<\/font><font color=\"#808080\">       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <\/font><font color=\"#2b91af\">$Shares<\/font><font color=\"#000000\">.Create(\u201cC:\\NetworkShare\u201d,\u201dJoey\u201d,<\/font><font color=\"#800000\">0<\/font><font color=\"#000000\">)<\/font><font color=\"#808080\">       <br \/><\/font><font color=\"#000000\">}<\/font> <\/div>\n<\/blockquote>\n<p>Now that we know which pieces work (because remember at this point we\u2019re typing live commands into a shell), we can pull out our favorite editor and build that into a useful daily function<\/p>\n<p><b><i><\/i><\/b><\/p>\n<blockquote>\n<div class=\"code\"><font color=\"#000000\">Function<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">NEW-SHARE<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">(<\/font><font color=\"#2b91af\">$Foldername<\/font><font color=\"#000000\">,<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#2b91af\">$Sharename<\/font><font color=\"#000000\">)<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">{<\/font><font color=\"#808080\">       <\/p>\n<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <\/font><font color=\"#0000ff\">#<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">Test<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">for<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">existence<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">of<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">folder,<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#0000ff\">if<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">not<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">there<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">then<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">create<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">it<\/font><font color=\"#808080\">       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <\/font><font color=\"#0000ff\">#<\/font><font color=\"#808080\">       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <\/font><font color=\"#0000ff\">IF<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">(!(<\/font><font color=\"#0000ff\">TEST-PATH<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#2b91af\">$Foldername<\/font><font color=\"#000000\">))<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">{<\/font><font color=\"#808080\">       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <\/font><font color=\"#0000ff\">NEW-ITEM<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#2b91af\">$Foldername<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">-type<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">Directory<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">}<\/font><font color=\"#808080\">       <\/p>\n<p><\/font><font color=\"#0000ff\">#<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">Create<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">Share<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">but<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">check<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">to<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">make<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">sure<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">it<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">isn\u2019t<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">already<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">there<\/font><font color=\"#808080\">       <br \/><\/font><font color=\"#0000ff\">#<\/font><font color=\"#808080\">       <br \/><\/font><font color=\"#0000ff\">If<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">(!(<\/font><font color=\"#0000ff\">GET-WMIOBJECT<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">Win32_Share<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">-filter<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">\u201cname<\/font><font color=\"#0000ff\">=<\/font><font color=\"#2b91af\">$Sharename\u201d<\/font><font color=\"#000000\">)<\/font><font color=\"#808080\">&#160;<\/font><font color=\"#000000\">{<\/font><font color=\"#808080\">       <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <\/font><font color=\"#2b91af\">$Shares<\/font><font color=\"#000000\">.Create(<\/font><font color=\"#2b91af\">$Foldername<\/font><font color=\"#000000\">,<\/font><font color=\"#2b91af\">$Sharename<\/font><font color=\"#000000\">,<\/font><font color=\"#800000\">0<\/font><font color=\"#000000\">)<\/font><font color=\"#808080\">       <br \/><\/font><font color=\"#000000\">}<\/font><font color=\"#808080\">       <\/p>\n<p><\/font><font color=\"#000000\">}<\/font> <\/div>\n<\/blockquote>\n<p>There! Now we can have a field day with creating shares! One simple function in Windows PowerShell that will do all the work for us. Of course we could <em>dramatically <\/em>improve this function by working on some remoting, providing feedback to the administrator, permissions, and perhaps even\n have it playing a little song and show some pretty lights.<\/p>\n<p>But, as Hammy the Hamster would say, that\u2019s another story. <\/p>\n<p>&#160;<\/p>\n<p>SK, that is all there is to using Windows PowerShell to create shares. Quick-Hits Friday is on deck tomorrow when we will talk about a variety of things. We ask you to follow us on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send email to us at <a href=\"mailto:scripter@microsoft.com\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p>&#160;<\/p>\n<p><b>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#160; Summary: Guest blogger Sean Kearney shows you how to use Windows PowerShell to create shared folders and set permissions on a Windows desktop. &#160; Hey, Scripting Guy! How can I use Windows PowerShell to create shares? &#8212; SK &#160; Hello SK, Microsoft Scripting Guy Ed Wilson here. Wrapping up Guest Blogger Week is Sean [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[56,3,154,185,12,45],"class_list":["post-17101","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-guest-blogger","tag-scripting-guy","tag-sean-kearney","tag-shared-folders-and-mapped-drives","tag-storage","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>&#160; Summary: Guest blogger Sean Kearney shows you how to use Windows PowerShell to create shared folders and set permissions on a Windows desktop. &#160; Hey, Scripting Guy! How can I use Windows PowerShell to create shares? &#8212; SK &#160; Hello SK, Microsoft Scripting Guy Ed Wilson here. Wrapping up Guest Blogger Week is Sean [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/17101","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\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=17101"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/17101\/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=17101"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=17101"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=17101"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}