SoFunction
Updated on 2025-04-08

Scripts that keep the command window open after running the command line tool in vbs

ask:
Hello, script expert! How to keep the command window open after running a tool like Ping or Ipconfig?
-- DB
answer:
Hello, DB. This question reminds us of the past. There was a scripting expert who just came to Microsoft, and many people thought that WMI and ADSI were too difficult for scriptwriters to use. Therefore, it is recommended that this script expert not use WMI or ADSI, but simply use VBScript as a way to call command line tools. In fact, the first chapter written by this scripting expert was a chapter on event log management, which later became part of the Microsoft Windows 2000 scripting guide. It is also a chapter that does not contain any scripting code or something.
You're right: Only script experts will find themselves writing a scripting guide that absolutely does not contain any scripting code.
Of course, script experts use eloquent persuasion and combined with a lot of begging, and ultimately convince people that it would be OK if the thing called Microsoft Windows 2000 Script Guide actually contains one or two scripts. However, during this process, script experts also have a little understanding of calling command line tools within the script, which is why we can answer your question.
We guess you have a script similar to the following that runs the command line tool:
Set objShell = CreateObject("")
("ipconfig /all")
You must know that the script is very effective: the command window pops up, and then Ipconfig starts running. The only problem is that before you can read the information returned by IPconfig, the command window will be closed (unless you read it really fast). This is indeed a problem.
So how to solve it? The method is as follows:
Set objShell = CreateObject("")
("%comspec% /k ipconfig /all")
As you can see, this revised script retains the same basic structure as the original script: we create an instance of the object and then call the Run method to actually run the command line tool. The difference is the way the command line tool is called. In the original script, we just call the tool itself:
("ipconfig /all")
This time, the syntax we used was very different:
("%comspec% /k ipconfig /all")
The environment variable %comspec% represents the Windows command shell; this is equivalent to calling (it will of course open a command window). Then why not call it directly? That's it, suppose your computer is running Windows 98. On these computers, the command shell is called by running, because there is no. Using %comspec% helps to ensure that the command window is available regardless of the version of Windows that script is running on.
In other words, when using this script, we do not run Ipconfig directly, but run an instance of the command shell and pass several parameters to the instance. The first of such parameters is /k, which instructs the command shell to perform the tasks we ask it to complete and then stay open. (We learned that k is the abbreviation of keep, like in "keep open", but we cannot be sure whether it is true.) We can also use the parameter /c (c stands for close), which automatically closes the command window when the command window completes the task.
What are the other parameters passed to the command shell? Those parameters are just the commands required to run IPconfig: ipconfig/all. Want to use the ping command to execute the IP address 192.168.1.1? The following script will perform this operation and ensure that the command window remains open afterwards:
Set objShell = CreateObject("")
("%comspec% /k ping 192.168.1.1")
Want to run  to get a list of local user accounts and can you view it later? OK, no problem:
Set objShell = CreateObject("")
("%comspec% /k net user")
Want¨C Well, you have understood the key point.
If you tend to use command line tools heavily in your scripts (this is nothing wrong; please use what you think is the most convenient/best tool), you might be interested in this column, which tells you how to change the command window title and command window color. Over the years, script experts still have a good impression of calling command line tool scripts, and it is still impossible to figure out why people think it is a good idea to write scripting guides that don't contain any scripts at all.