{"id":67733,"date":"2006-03-17T18:55:00","date_gmt":"2006-03-17T18:55:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/03\/17\/how-can-i-start-windows-explorer-opened-to-a-specific-folder\/"},"modified":"2006-03-17T18:55:00","modified_gmt":"2006-03-17T18:55:00","slug":"how-can-i-start-windows-explorer-opened-to-a-specific-folder","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-start-windows-explorer-opened-to-a-specific-folder\/","title":{"rendered":"How Can I Start Windows Explorer Opened to a Specific Folder?"},"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 start Windows Explorer opened to a specific folder?<BR><BR>&#8212; CD<\/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, CD. We\u2019re going to let you in on one of the Scripting Guys\u2019 most sordid secrets: sometimes the Scripting Guys don\u2019t agree with each other. No, it\u2019s true; really. For example, some of the Scripting Guys think that soccer is a real sport; even worse, at least one of the Scripting Guys thinks that <I>stock car racing<\/I> is a real sport! Oh, brother.<\/P>\n<TABLE class=\"dataTable\" id=\"E3C\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Note<\/B>. Following a brief \u2026 get-together \u2026 the Scripting Guy who writes this column has agreed that he was wrong and that both soccer and stock car racing are real sports after all. Sort of.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Here\u2019s another area in which the Scripting Guys disagree. At least one of the Scripting Guys believes that system administrators and IT professionals would <I>never<\/I> stoop to using the Windows graphical user interface (GUI). And he\u2019s not the only Microsoft-type who feels that way; we know of at least one Program Manager who, whenever he wants to start Word, opens a command window and types <B>winword.exe<\/B> at the command prompt. Other Scripting Guys would argue that system administrators don\u2019t care whether they use the command line or a GUI tool: they just want to get the job done as quickly and as easily as possible.<\/P>\n<P>So is one side right and one side wrong? Of course! However, just as we \u2026 agreed \u2026 that soccer and stock car racing were sports, we\u2019ve also agreed that the command line and the GUI are equally good ways of getting Windows Explorer to open up with the focus on a specific folder. Therefore, we\u2019re going to give you two different scripts to choose from, one command-line oriented, one GUI-oriented.<\/P>\n<P>Let\u2019s start with the command-line script first. To get this script (which we\u2019ll call my-script.vbs) to work all you need to do is call the script from the command prompt, passing the name of the folder you want opened as the lone argument. In other words, to open Windows Explorer focused on C:\\Scripts you\u2019d type the following from the command prompt:<\/P><PRE class=\"codeSample\">my-script.vbs &#8220;c:\\scripts&#8221;\n<\/PRE>\n<P>Are the double quotes required around the folder path? In this case, no. However, if there are any blank spaces in the path then the double quotes <I>are<\/I> required. This command string won\u2019t work:<\/P><PRE class=\"codeSample\">my-script.vbs c:\\documents and settings\\kmyer\n<\/PRE>\n<P>Any time you pass a script an argument that includes a blank space the entire argument must be enclosed in double quotes (or else). In other words:<\/P><PRE class=\"codeSample\">my-script.vbs &#8220;c:\\documents and settings\\kmyer&#8221;\n<\/PRE>\n<P>That\u2019s just the way the command shell works.<\/P>\n<P>So what script are we running here? Good question. Turns out it\u2019s this one:<\/P><PRE class=\"codeSample\">Set objShell = CreateObject(&#8220;Wscript.Shell&#8221;)<\/p>\n<p>strPath = Wscript.Arguments(0)<\/p>\n<p>strPath = &#8220;explorer.exe \/e,&#8221; &amp; strPath\nobjShell.Run strPath\n<\/PRE>\n<P>As usual, there\u2019s really not that much to this. We start out by creating an instance of the <B>Wscript.Shell<\/B> object; this is the Windows Script Host object we can use to run scripts or executable files from within another script. We then take the first argument supplied to the script (c:\\scripts) and stash that in a variable named strPath:<\/P><PRE class=\"codeSample\">strPath = Wscript.Arguments(0)\n<\/PRE>\n<P>So far so good, huh? In a moment, we\u2019re going to use the <B>Run<\/B> method to start Windows Explorer. Before we do that, however, we should note that the Run method in WSH is pretty much the same thing as the <B>Run<\/B> dialog box. If you wanted to use the Run dialog box to start Windows Explorer (with the focus on the C:\\Scripts folder) you\u2019d type in this:<\/P><PRE class=\"codeSample\">explorer.exe \/e,c:\\scripts\n<\/PRE>\n<P>As it turns out, that\u2019s the same syntax we use to start Windows Explorer using the Run method. All we have to do is construct that command and then execute it: <\/P><PRE class=\"codeSample\">strPath = &#8220;explorer.exe \/e,&#8221; &amp; strPath\nobjShell.Run strPath\n<\/PRE>\n<P>In line 1 we\u2019re taking the command <B>explorer.exe \/e,<\/B> and tacking on the folder path (which is stored in the variable strPath); strPath will then take on the value <B>explorer.exe \/e,c:\\scripts<\/B>. In line 2 we call the <B>Run<\/B> method, passing the variable strPath as the command we want to run. If all goes well (and in scripting things always go well, right?), Windows Explorer will open up with the focus on C:\\Scripts:<\/P><IMG height=\"286\" alt=\"Hey, Scripting Guy!\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/fbrowse1.jpg\" width=\"400\" border=\"0\"> \n<P><BR>Cool.<\/P>\n<P>So what\u2019s wrong with that? Nothing; it works just fine. The only disadvantage to this script is the fact that it requires you to type in the full path to the folder; that can be a problem when you\u2019re trying to open the folder <B>C:\\Documents and Settings\\Default User\\Application Data\\Microsoft\\SystemCertificates\\My\\Certificates<\/B>. But as system administrators wedded to the command line, what choice to we have?<\/P>\n<P>Well, you could always try the GUI approach (don\u2019t worry; we won\u2019t tell anyone):<\/P><PRE class=\"codeSample\">Const WINDOW_HANDLE = 0\nConst NO_OPTIONS = 0<\/p>\n<p>Set objShell = CreateObject(&#8220;Shell.Application&#8221;)\nSet objFolder = objShell.BrowseForFolder _\n    (WINDOW_HANDLE, &#8220;Select a folder:&#8221;, NO_OPTIONS)       <\/p>\n<p>Set objFolderItem = objFolder.Self\nstrPath = objFolderItem.Path<\/p>\n<p>objShell.Explore strPath\n<\/PRE>\n<P>This script doesn\u2019t require you to do any typing whatsoever. Instead, you simply start the script and it shows you the <B>Browse For Folder<\/B> dialog box:<\/P><IMG height=\"374\" alt=\"Hey, Scripting Guy!\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/fbrowse2.jpg\" width=\"326\" border=\"0\"> \n<P><BR>Select a folder, click <B>OK<\/B>, and you\u2019re on your way.<\/P>\n<P>So how does this script work? Well, we start out by defining a pair of constants: WINDOW_HANDLE and NO_OPTIONS. WINDOW_HANDLE is a constant required by the <B>BrowseForFolder<\/B> method; NO_OPTIONS simply informs the script that we want to display a standard Browse For Folder dialog box. After that we create an instance of the <B>Shell.Application<\/B> object and then use this line of code to display the dialog box:<\/P><PRE class=\"codeSample\">Set objFolder = objShell.BrowseForFolder _\n    (WINDOW_HANDLE, &#8220;Select a folder:&#8221;, NO_OPTIONS)\n<\/PRE>\n<P>After the dialog box is displayed it will simply wait until we select a folder and then click <B>OK<\/B>. When that happens we use these two lines of code to: 1) create an object reference to the selected folder; and, 2) store the folder <B>Path<\/B> in the variable strPath:<\/P><PRE class=\"codeSample\">Set objFolderItem = objFolder.Self\nstrPath = objFolderItem.Path\n<\/PRE>\n<P>All we have to do now is call the <B>Explore<\/B> method, which exists solely to open Windows Explorer. By passing the variable strPath as the lone parameter that will cause Windows Explorer to open up with the focus on C:\\Scripts:<\/P><PRE class=\"codeSample\">objShell.Explore strPath\n<\/PRE>\n<P>The moral of the story? Now you can use the command prompt to open up Windows Explorer or you can use the GUI to open Windows Explorer; it\u2019s entirely up to you. And if you want to watch soccer or stock car racing the next time you feel like watching a sporting event, well, so be it. (Of course, you won\u2019t be watching a <I>real<\/I> sporting event, but \u2026.)<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I start Windows Explorer opened to a specific folder?&#8212; CD Hey, CD. We\u2019re going to let you in on one of the Scripting Guys\u2019 most sordid secrets: sometimes the Scripting Guys don\u2019t agree with each other. No, it\u2019s true; really. For example, some of the Scripting Guys think that soccer [&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,3,5,226],"class_list":["post-67733","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-desktop-management","tag-scripting-guy","tag-vbscript","tag-windows-explorer"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I start Windows Explorer opened to a specific folder?&#8212; CD Hey, CD. We\u2019re going to let you in on one of the Scripting Guys\u2019 most sordid secrets: sometimes the Scripting Guys don\u2019t agree with each other. No, it\u2019s true; really. For example, some of the Scripting Guys think that soccer [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67733","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=67733"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67733\/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=67733"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=67733"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=67733"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}