{"id":65013,"date":"2007-04-24T21:26:00","date_gmt":"2007-04-24T21:26:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/04\/24\/how-can-i-get-the-command-window-to-stay-open-when-i-right-click-a-script-and-choose-open-with-command-prompt\/"},"modified":"2007-04-24T21:26:00","modified_gmt":"2007-04-24T21:26:00","slug":"how-can-i-get-the-command-window-to-stay-open-when-i-right-click-a-script-and-choose-open-with-command-prompt","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-get-the-command-window-to-stay-open-when-i-right-click-a-script-and-choose-open-with-command-prompt\/","title":{"rendered":"How Can I Get the Command Window to Stay Open When I Right-Click a Script and Choose Open With Command Prompt?"},"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 get the command window to stay open when I right-click a .VBS file and choose <B>Open With Command Prompt<\/B>??<BR><BR>&#8212; BB<\/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, BB. You know, according to BB\u2019s email signature, he\u2019s 9 years old. If that\u2019s true, well, that\u2019s absolutely amazing. A 9-year-old who knows VBScript? At age 9 the Scripting Guys could barely feed themselves or tie their own shoes.<\/P>\n<P>Which, in their case, simply means that the more things change the more they stay the same.<\/P>\n<P>Of course, we have to admit that, just because BB <I>says<\/I> he\u2019s 9 years old doesn\u2019t mean he really <I>is<\/I> 9 years old. After all, the Scripting Editor continues to insist that she\u2019s 23 years old. But how, then, can she explain the photographs that clearly show her attending Franklin Delano Roosevelt\u2019s Inauguration Ball in 1937? The truth is, she can\u2019t. <\/P>\n<P>Instead, she just throws things any time the subject is broached.<\/P>\n<P>Anyway, we can\u2019t be sure that BB really <I>is<\/I> 9 years old, but we\u2019ll try to figure out a way to verify that, maybe by asking him a question that only a 9-year-old would know the answer to. You know, like this one: name all four Ninja Turtles.<\/P>\n<P>Good point; after all, even the Scripting Guy who writes this column knows the answer to <I>that<\/I>: Leonardo, Donatello, Michelangelo, and Raphael. And that\u2019s without using the Internet to look up the answer.<\/P>\n<TABLE class=\"dataTable\" id=\"ESD\" 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>. Actually, the Scripting Guy who writes this column <I>never<\/I> uses the Internet, or anything else, to look up an answer. He just assumes he knows everything.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Of course, whether BB is really 9 years old or not doesn\u2019t matter; either way he has a good question here. If you right-click a .VBS file one of the options available to you on the context menu is this: <B>Open With Command Prompt<\/B>. If you choose that option <I>something<\/I> seems to happen: typically a command window will briefly flash onscreen and then just as quickly disappear. And that\u2019s it. Why doesn\u2019t the script actually run, and why doesn\u2019t the command window stay open?<\/P>\n<P>Let\u2019s take the first question first. (Kind of an unusual approach for <I>Hey, Scripting Guy!<\/I>, isn\u2019t it?) As it turns out, the script actually <I>does<\/I> run; it\u2019s just that many VBScripts execute so quickly that you don\u2019t realize that anything happened. (About the same time the command window appears onscreen the script has completed its tasks and it\u2019s now time for the command window to close.) Here\u2019s one way to verify this. Create a simple script that does nothing more than display a message box:<\/P><PRE class=\"codeSample\">Msgbox &#8220;Hey&#8221;\n<\/PRE>\n<P>Now, right-click this .VBS file and choose <B>Open With Command Prompt<\/B>. You should see the message box, indicating that the script really <I>did<\/I> run after all.<\/P>\n<P>So then why didn\u2019t the command window stay open? That\u2019s easy: by default the command window tends to do its thing and then disappear. For example, open the <B>Run<\/B> dialog box, type <B>ipconfig<\/B>, and then press ENTER. You\u2019ll see a command window open and \u2013 if you\u2019ve got good eyesight \u2013 you might even see that Ipconfig.exe has run. But the very instant that Ipconfig finishes, the command window automatically closes. That\u2019s why you\u2019re usually instructed to first open a command window and <I>then<\/I> run a script or command-line tool. If you don\u2019t the script or tool will run, but you\u2019ll probably never see the output.<\/P>\n<P>Is that a problem, having to open a command window and then run the script from within that window? Of course it is; after all, that pretty much defeats the whole idea of having an Open With Command Prompt shortcut in the first place.<\/P>\n<P>But we have a good news for you, BB: if you don\u2019t like the way the Open With Command Prompt shortcut works then you can go ahead and change the behavior of this shortcut. How do you do that? Good question; might we suggest using a script like this one:<\/P><PRE class=\"codeSample\">Const HKEY_CLASSES_ROOT = &amp;H80000000<\/p>\n<p>strComputer = &#8220;.&#8221;<\/p>\n<p>Set objRegistry=GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\default:StdRegProv&#8221;)<\/p>\n<p>strKeyPath = &#8220;VBSFile\\Shell\\Open2\\Command&#8221;\nstrValueName = &#8220;&#8221;\nstrValue = &#8220;cmd.exe \/K cscript.exe &#8221; &amp; Chr(34) &amp; &#8220;%1&#8243; &amp; Chr(34) &amp; &#8221; %*&#8221; <\/p>\n<p>objRegistry.SetExpandedStringValue _\n    HKEY_CLASSES_ROOT,strKeyPath,strValueName,strValue\n<\/PRE>\n<P>As it turns out, the behavior of the Open With Command Prompt shortcut is dictated by a setting in the registry (HKEY_CLASSES_ROOT\\VBSFile\\Shell\\Open2\\Command). By default, this setting simply calls the CScript or WScript script engine (depending on your preferred settings), passing along the file path as a command-line argument:<\/P><PRE class=\"codeSample\">%SystemRoot%\\System32\\CScript.exe &#8220;%1&#8221; %*\n<\/PRE>\n<P>If we want the command window to stay open any time we run a script using this shortcut all we have to do is modify the registry setting. How do we do that? We were just about to get to that.<\/P>\n<P>If you take a look at our script you\u2019ll see that we start things off by defining a constant named HKEY_CLASSES_ROOT and setting the value to &amp;H80000000; we\u2019ll use this constant to tell the script which registry hive we want to work with. We then connect to the WMI service on the local computer, binding to the <B>StdRegProv<\/B> class. (Which, we should point out, resides in the <B>root\\default<\/B> namespace.)<\/P>\n<TABLE class=\"dataTable\" id=\"ESF\" 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>. Can you run this script against remote computers? Of course you can; all you have to do is assign the name of the remote computer to the variable strComputer.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>After connecting to the WMI service we then assign values to three variables:<\/P>\n<TABLE class=\"\" cellSpacing=\"0\" cellPadding=\"0\" border=\"0\">\n<TBODY>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>strKeyPath<\/B>, which represents the registry path within the HKEY_CLASSES_ROOT hive. In this case we set the path to VBSFile\\Shell\\Open2\\Command.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>strValueName<\/B>, which represents the actual registry value we want to modify. As it turns out, we want to modify the default value, a value that shows up in Regedit like this: (Default). How do we indicate that we want to work with this default value? That\u2019s easy: we just assign an empty string (\u201c\u201d) to strValueName.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>strValue<\/B>, which represents the new value to be written to the registry.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>Let\u2019s talk a little bit about strValue. As we noted earlier, the command window tends to open up, do its thing, and then close. However, there <I>is<\/I> a way to keep the command window open after it completes its appointed tasks: you just have to use the <B>\/K<\/B> (keep) switch when opening the window. And that\u2019s exactly what we\u2019re doing here. Notice that, this time, we aren\u2019t directly starting Cscript.exe. Instead, we\u2019re opening up a command window (Cmd.exe) and using the \/K switch to ensure that the window stays open. That\u2019s what this portion of our code is for:<\/P><PRE class=\"codeSample\">cmd.exe \/K\n<\/PRE>\n<P>We then pass the name of the script host (CScript.exe) and the file path as additional command-line arguments for Cmd.exe. In other words, any time someone selects Open With Command Prompt we\u2019re going to open a persistent version of the command window, then tell the command window that \u2013 when it opens \u2013 it needs to run CScript.exe and the selected .VBS file.<\/P>\n<P>Confusing? Yes, it is, at least a little. And the syntax of our command \u2013 which includes the function <B>Chr(34)<\/B>, something we use to insert double quote marks into the string value \u2013 doesn\u2019t help much. The long and short of it, however, is that strValue ends up being assigned this value:<\/P><PRE class=\"codeSample\">cmd.exe \/K cscript.exe &#8220;%1&#8221; %*\n<\/PRE>\n<P>And, believe it or not, that\u2019s the command that will \u201cfix\u201d the Open With Command Prompt shortcut.<\/P>\n<P>In fact, after assigning values to our variables all we have to do is call the <B>SetExpandedStringValue<\/B> method (because our registry value happens to have the REG_EXPAND_SZ data type) and we\u2019re done:<\/P><PRE class=\"codeSample\">objRegistry.SetExpandedStringValue _\n    HKEY_CLASSES_ROOT,strKeyPath,strValueName,strValue\n<\/PRE>\n<P><I>Now<\/I> try right-clicking a .VBS file and selecting <B>Open With Command Prompt<\/B>. This time a command window should open, the script should run, and \u2013 upon completion \u2013 the command window should stay open. Pretty sweet, huh?<\/P>\n<P>Again, BB, if you really are 9 years old the Scripting Guys are impressed. Or at least the Scripting Guy who writes this column is impressed. After all, was the Scripting Guy who writes this column fooling around with VBScript when he was 9? No he wasn\u2019t, primarily because neither VBScript nor the command prompt had been invented way back then. <\/P>\n<P>In fact, come to think, the number <I>9<\/I> hadn\u2019t even been invented yet back when the Scripting Guy who writes this column was a kid. Wow \u2026.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I get the command window to stay open when I right-click a .VBS file and choose Open With Command Prompt??&#8212; BB Hey, BB. You know, according to BB\u2019s email signature, he\u2019s 9 years old. If that\u2019s true, well, that\u2019s absolutely amazing. A 9-year-old who knows VBScript? At age 9 the [&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":[2,3,4,5],"class_list":["post-65013","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-running","tag-scripting-guy","tag-scripting-techniques","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I get the command window to stay open when I right-click a .VBS file and choose Open With Command Prompt??&#8212; BB Hey, BB. You know, according to BB\u2019s email signature, he\u2019s 9 years old. If that\u2019s true, well, that\u2019s absolutely amazing. A 9-year-old who knows VBScript? At age 9 the [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65013","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=65013"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65013\/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=65013"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=65013"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=65013"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}