{"id":68543,"date":"2005-11-11T13:58:00","date_gmt":"2005-11-11T13:58:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/11\/11\/how-can-i-get-the-command-window-to-stay-open-after-running-a-command-line-tool\/"},"modified":"2005-11-11T13:58:00","modified_gmt":"2005-11-11T13:58:00","slug":"how-can-i-get-the-command-window-to-stay-open-after-running-a-command-line-tool","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-get-the-command-window-to-stay-open-after-running-a-command-line-tool\/","title":{"rendered":"How Can I Get the Command Window to Stay Open After Running a Command-Line Tool?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" 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\"> \n<P>Hey, Scripting Guy! How can I get the command window to stay open after running a tool like Ping or Ipconfig?<BR><BR>&#8212; DB<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" 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\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>Hey, DB. Now <I>this<\/I> question takes us back. Back when one of the Scripting Guys first started here at Microsoft many people thought WMI and ADSI were way too hard for script writers to use. As a result, this Scripting Guy was discouraged from using WMI or ADSI; instead, he was urged to simply use VBScript as a way to call command-line tools. In fact, the first chapter this Scripting Guy ever drafted for what became the <A href=\"http:\/\/null\/technet\/scriptcenter\/guide\/default.mspx\" target=\"_blank\"><B><I>Microsoft Windows 2000 Scripting Guide<\/I><\/B><\/A> was a chapter on managing event logs. It was also a chapter that had no scripting code whatsoever.<\/P>\n<P>And you\u2019re right: only the Scripting Guys could find themselves working on a scripting guide that included absolutely no scripting code.<\/P>\n<P>Eventually, of course, the Scripting Guys &#8211; using a mixture of a little eloquent persuasion and a lot of whining and crying &#8211; were able to convince people that maybe it would be OK if something called the <I>Microsoft Windows 2000 Scripting Guide<\/I> actually had a script or two in it. Along the way, though, the Scripting Guys also learned a thing or two about calling command-line tools from within a script, which is why we\u2019re able to answer your question.<\/P>\n<P>We\u2019re guessing that you have a script similar to this one, a script which runs the command-line tool Ipconfig.exe:<\/P><PRE class=\"codeSample\">Set objShell = CreateObject(&#8220;Wscript.Shell&#8221;)\nobjShell.Run(&#8220;ipconfig \/all&#8221;)\n<\/PRE>\n<P>As you doubtless know, this script works just fine: a command window pops up and Ipconfig runs. The only problem is that the command window then closes before you can actually read the information IPconfig returned (unless you read <I>really<\/I> fast). That\u2019s definitely a problem.<\/P>\n<P>So how do we fix it? Here\u2019s how:<\/P><PRE class=\"codeSample\">Set objShell = CreateObject(&#8220;Wscript.Shell&#8221;)\nobjShell.Run(&#8220;%comspec% \/k ipconfig \/all&#8221;)\n<\/PRE>\n<P>As you can see, this revised script retains the same basic structure as our original script: we create an instance of the <B>Wscript.Shell<\/B> object and then call the <B>Run<\/B> method to actually run the command-line tool. The difference lies in the way we call that command-line tool. In the original script we simply called the tool itself:<\/P><PRE class=\"codeSample\">objShell.Run(&#8220;ipconfig \/all&#8221;)\n<\/PRE>\n<P>This time around, we use a very different syntax:<\/P><PRE class=\"codeSample\">objShell.Run(&#8220;%comspec% \/k ipconfig \/all&#8221;)\n<\/PRE>\n<P>The environment variable <I>%comspec%<\/I> represents the Windows command shell; this is equivalent to calling Cmd.exe (which, of course, would open up a command window). So why don\u2019t we just <I>call<\/I> Cmd.exe? Well, suppose you have computers running Windows 98. On those machines the command shell is invoked by running Command.com; there <I>is<\/I> no Cmd.exe. Using <I>%comspec%<\/I> helps ensures that we\u2019ll get a command window regardless of the version of Windows that the script is running on.<\/P>\n<P>In other words, with this script we aren\u2019t directly running Ipconfig; instead, we\u2019re running an instance of the command shell, and passing that instance several parameters. The first such parameter is <B>\/k<\/B>; this parameter tells the command shell to do whatever we ask it to and then <I>remain open<\/I>. (We\u2019ve been told that the k is short for <B>k<\/B>eep, as in \u201ckeep open\u201d but we aren\u2019t sure whether that\u2019s true or not.) Alternatively, we could have used the parameter <B>\/c <\/B>(c for <B>c<\/B>lose), which would have automatically closed the command window at the completion of its task. <\/P>\n<P>What about the other parameters passed to the command shell? Those are simply the commands needed to run IPconfig:<B> ipconfig \/all<\/B>. Want to use Ping.exe to ping IP address 192.168.1.1? This script will do that, and ensure that the command window stays open afterwards:<\/P><PRE class=\"codeSample\">Set objShell = CreateObject(&#8220;Wscript.Shell&#8221;)\nobjShell.Run(&#8220;%comspec% \/k ping 192.168.1.1&#8221;)\n<\/PRE>\n<P>Want to run Net.exe in order to get &#8211; and then be able to view &#8211; a list of local user accounts? Okey-doke:<\/P><PRE class=\"codeSample\">Set objShell = CreateObject(&#8220;Wscript.Shell&#8221;)\nobjShell.Run(&#8220;%comspec% \/k net user&#8221;)\n<\/PRE>\n<P>Want to &#8211; well, you get the idea.<\/P>\n<P>If you tend to use command-line tools a lot in your scripts (and there\u2019s nothing wrong with that; use whatever you find easiest\/best) you might be interested in <A href=\"http:\/\/null\/technet\/scriptcenter\/resources\/qanda\/aug04\/hey0826.mspx\"><B>this column<\/B><\/A>, which tells you how to change both the command window title and the command window colors. After all these years the Scripting Guys still have a soft spot in their hearts for scripts that call command-line tools. And still can\u2019t figure out why people thought it was a good idea to write a scripting guide that didn\u2019t have any scripts.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I get the command window to stay open after running a tool like Ping or Ipconfig?&#8212; DB Hey, DB. Now this question takes us back. Back when one of the Scripting Guys first started here at Microsoft many people thought WMI and ADSI were way too hard for script writers [&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-68543","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 after running a tool like Ping or Ipconfig?&#8212; DB Hey, DB. Now this question takes us back. Back when one of the Scripting Guys first started here at Microsoft many people thought WMI and ADSI were way too hard for script writers [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68543","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=68543"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68543\/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=68543"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=68543"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=68543"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}