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

ScriptingGuy1

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 Command.com; 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 192.168.1.1? This script will do that, and ensure that the command window stays open afterwards:

Set objShell = CreateObject(“Wscript.Shell”)
objShell.Run(“%comspec% /k ping 192.168.1.1”)

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.

0 comments

Discussion is closed.

Feedback usabilla icon