{"id":64603,"date":"2007-06-22T00:11:00","date_gmt":"2007-06-22T00:11:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/06\/22\/how-can-i-open-a-window-to-a-unc-location\/"},"modified":"2007-06-22T00:11:00","modified_gmt":"2007-06-22T00:11:00","slug":"how-can-i-open-a-window-to-a-unc-location","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-open-a-window-to-a-unc-location\/","title":{"rendered":"How Can I Open a Window to a UNC Location?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> \n<P>Hey, Scripting Guy! How can I open a window to a UNC location? You&#8217;ve given many examples that involve opening a web page. I have been unable to get a similar result trying to get a script to open \\\\server\\share.<BR><BR>&#8212; CY <\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\"><\/A> \n<P>Hey, CY. You know, during the heyday of the Cold War the U.S. had analysts whose sole duty was to pore over photographs taken in the Soviet Union and try to figure out what was <I>really<\/I> going on behind the Iron Curtain. Who was standing next to whom? Who had been relegated to the background? Who had been airbrushed out of the picture altogether? Just what <I>was<\/I> going on over there?<\/P>\n<P>Today those same analysts spend their time poring over <I>Hey, Scripting Guy! <\/I>columns. <I>Would<\/I> the Scripting Guys \u201c<A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/jul06\/hey0714.mspx\"><B>tell you porkies?<\/B><\/A>\u201d What <I>was<\/I><A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/sept05\/hey0901.mspx\"><B>Frank \u201cHome Run\u201d Baker<\/B><\/A> known for besides leading major league baseball in home runs four times? Can you <I>really<\/I><A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/may07\/hey0503.mspx\"><B>make scrambled eggs<\/B><\/A> just by shaking a chicken really hard right before she lays her eggs? Just what <I>is<\/I> going on over there?<\/P>\n<P>Needless to say, we can\u2019t reveal <I>all<\/I> our secrets. But here\u2019s a tip. Sometimes you\u2019ll hear the Scripting Guys say something like, \u201cYou know, this is something we get asked quite a bit and, fortunately, the solution is quite simple.\u201d They\u2019ll then proceed to show everyone a two-line script. Does that mean that this is a question that the Scripting Guys really <I>do<\/I> get asked quite a bit? Well, maybe. What it mostly means, however, is that the Scripting Guys are feeling lazy that day and are trying to get away with writing two lousy lines of code and calling it a column. <I>Don\u2019t let them get away it!<\/I> As loyal readers of <I>Hey, Scripting Guy!<\/I> you deserve better than that. <\/P>\n<P>With that out of the way, let\u2019s see if we can tackle today\u2019s problem: opening a window to a UNC location. You know, this is something we get asked quite a bit and, fortunately, the solution is quite simple. In fact, this task can be carried out using just two lines of code:<\/P><PRE class=\"codeSample\">Set objShell = CreateObject(&#8220;Wscript.Shell&#8221;)\nobjShell.Run &#8220;\\\\atl-fs-01\\public&#8221;\n<\/PRE>\n<P>As you can see, there isn\u2019t much to this script. We start out by creating an instance of the <B>Wscript.Shell<\/B> object; once we have a handle to that object all we have to do is call the <B>Run<\/B> method, passing our UNC path as the sole method parameter. Will that really open \\\\atl-fs-01\\public in a Windows Explorer window? Give it a try and see for yourself.<\/P>\n<P>The only thing you need to watch out for are UNC paths that include blank spaces. For example, consider the following script, one that purports to open the folder <B>\\\\atl-fs-01\\public\\ken myer<\/B>:<\/P><PRE class=\"codeSample\">Set objShell = CreateObject(&#8220;Wscript.Shell&#8221;)\nobjShell.Run &#8220;\\\\atl-fs-01\\public\\ken myer&#8221;\n<\/PRE>\n<P>Is that going to work? Unfortunately, no; that\u2019s because Windows is going to assume that the path ends at the first blank space, making the path <B>\\\\atl-fs-01\\public\\ken<\/B>. All you\u2019re going to back is the following error message:<\/P><PRE class=\"codeSample\">The system cannot find the file specified.\n<\/PRE>\n<P>But that\u2019s OK; this is easy to fix. Any time your UNC path includes a blank space (or blank spaces) you need to enclose the entire path in double quote marks. (Yes, in much the same way you need to use double quote marks when typing a command like <B>cd \u201cc:\\documents and settings\\ken myer\u201d<\/B> from the command prompt.) Of course, it looks as though we already <I>have<\/I> double quote marks. However, those double quotes simply indicate that we are dealing with a string value; they aren\u2019t part of the value itself. This is crazy-looking, but the following syntax will take care of the blank space issue:<\/P><PRE class=\"codeSample\">Set objShell = CreateObject(&#8220;Wscript.Shell&#8221;)\nobjShell.Run &#8220;&#8221;&#8221;\\\\atl-fs-01\\public\\ken myer&#8221;&#8221;&#8221;\n<\/PRE>\n<P>Three double quote marks before the path and three double quote marks after the path. That\u2019s the recipe for success, in life as well as in scripting.<\/P>\n<P>Here\u2019s a little bonus script, one that we won\u2019t discuss in much detail. This script returns a collection of all the shared folders on a computer (in this case, a remote computer named atl-fs-01), taking care to eliminate all the administrative shares. (For our purposes, that means any shared folder that, like C$ or printers$, has a $ in the name.) After returning the collection the script constructs a UNC path for each shared folder and then opens that folder in its very own window:<\/P><PRE class=\"codeSample\">strComputer = &#8220;atl-fs-01&#8221;<\/p>\n<p>Set objShell = CreateObject(&#8220;Wscript.Shell&#8221;)<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)\nSet colItems = objWMIService.ExecQuery(&#8220;Select * From Win32_Share&#8221;)<\/p>\n<p>For Each objItem in colItems\n    If InStr(objItem.Name, &#8220;$&#8221;) = 0 Then\n        strPath = &#8220;\\\\&#8221; &amp; strComputer &amp; &#8220;\\&#8221; &amp; objItem.Name\n        objShell.Run strPath\n    End If\nNext\n<\/PRE>\n<P>Pretty slick, huh?<\/P>\n<P>And before we go, here\u2019s another handy hint for you. Any time the Scripting Guys start handing out bonus scripts that can mean only one thing: they\u2019re feeling a little guilty about not putting much effort into their work. Just something to watch out for.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I open a window to a UNC location? You&#8217;ve given many examples that involve opening a web page. I have been unable to get a similar result trying to get a script to open \\\\server\\share.&#8212; CY Hey, CY. You know, during the heyday of the Cold War the U.S. had [&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":[16,47,3,5],"class_list":["post-64603","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-desktop-management","tag-general-management-tasks","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I open a window to a UNC location? You&#8217;ve given many examples that involve opening a web page. I have been unable to get a similar result trying to get a script to open \\\\server\\share.&#8212; CY Hey, CY. You know, during the heyday of the Cold War the U.S. had [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64603","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=64603"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64603\/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=64603"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=64603"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=64603"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}