How Can I Get the Command Window to Stay Open After Running a Command-Line Tool?

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I get the command window to stay open after running a tool like Ping or Ipconfig?

-- DB

SpacerHey, Scripting Guy! AnswerScript Center

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 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 Microsoft Windows 2000 Scripting Guide was a chapter on managing event logs. It was also a chapter that had no scripting code whatsoever.

And you’re right: only the Scripting Guys could find themselves working on a scripting guide that included absolutely no scripting code.

Eventually, of course, the Scripting Guys - using a mixture of a little eloquent persuasion and a lot of whining and crying - were able to convince people that maybe it would be OK if something called the Microsoft Windows 2000 Scripting Guide 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’re able to answer your question.

We’re guessing that you have a script similar to this one, a script which runs the command-line tool Ipconfig.exe:

Set objShell = CreateObject("Wscript.Shell")
objShell.Run("ipconfig /all")

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 really fast). That’s definitely a problem.

So how do we fix it? Here’s how:

Set objShell = CreateObject("Wscript.Shell")
objShell.Run("%comspec% /k ipconfig /all")

As you can see, this revised script retains the same basic structure as our original script: we create an instance of the Wscript.Shell object and then call the Run 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:

objShell.Run("ipconfig /all")

This time around, we use a very different syntax:

objShell.Run("%comspec% /k ipconfig /all")

The environment variable %comspec% represents the Windows command shell; this is equivalent to calling Cmd.exe (which, of course, would open up a command window). So why don’t we just call Cmd.exe? Well, suppose you have computers running Windows 98. On those machines the command shell is invoked by running; there is no Cmd.exe. Using %comspec% helps ensures that we’ll get a command window regardless of the version of Windows that the script is running on.

In other words, with this script we aren’t directly running Ipconfig; instead, we’re running an instance of the command shell, and passing that instance several parameters. The first such parameter is /k; this parameter tells the command shell to do whatever we ask it to and then remain open. (We’ve been told that the k is short for keep, as in “keep open” but we aren’t sure whether that’s true or not.) Alternatively, we could have used the parameter /c (c for close), which would have automatically closed the command window at the completion of its task.

What about the other parameters passed to the command shell? Those are simply the commands needed to run IPconfig: ipconfig /all. Want to use Ping.exe to ping IP address This script will do that, and ensure that the command window stays open afterwards:

Set objShell = CreateObject("Wscript.Shell")
objShell.Run("%comspec% /k ping")

Want to run Net.exe in order to get - and then be able to view - a list of local user accounts? Okey-doke:

Set objShell = CreateObject("Wscript.Shell")
objShell.Run("%comspec% /k net user")

Want to - well, you get the idea.

If you tend to use command-line tools a lot in your scripts (and there’s nothing wrong with that; use whatever you find easiest/best) you might be interested in this column, 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’t figure out why people thought it was a good idea to write a scripting guide that didn’t have any scripts.

Comments (10)

  1. Ronald says:

    Thank you!

    This was extremely helpful and very easy to follow

  2. Vim says:

    How do you then get the window to stay open if you are running a tcl script. For example;

    set x 50

    set y 400

    puts stdout "$x+$y=[expr {$x+$y}]"

    puts stdout " The addition of two variables $x and $y is [expr $x+$y]"

  3. Vim says:

    This is because i was running into the same issue while trying to execute a tcl script using active state 8.6 in windows.


  4. kala says:

    Fat decrease doesn’t come simple and you must stay dedicated to maintaining a proper and balanced diet and training for at least an hour each day.

  5. selling continues. says:

    Launch of Home loan Fee – An formal papers to specify that your mortgagee has been compensated off by the selling continues.

  6. Managing says:

    Managing your earnings smartly is the most essential part on taruhan bola. Unfortunately, most individuals ignore this factor.

  7. check this site out says:

    After cleaning, planning is continued by cotton wool swab and sanding to discover reduce colour.

  8. best site says:

    One research revealed that when given to a number of seriously ill sufferers suffering from muscular spending, the sufferers actually passed away quicker and revealed no advantages from the treatment.

  9. article says:

    conformity with protection requirements, have good appearance and high quality, to meet the needs of the child’s potential.

  10. amanda says:

    Getting a command window after the command line tool gave me good knowledge and it really helped me to modify it in my, i really hope it would be successful in all the time as you run the script above with no errors.

Skip to main content