Learn about Windows PowerShell
Hey, Scripting Guy! I would like to be able to pause my script, then have it resume as soon as the user presses any key on the keyboard. How do I do that?-- AL
Hey, AL. Boy, this question takes us back: “Press any key to continue,” along with “Abort, Retry, or Fail,” has to be one of the most famous phrases in computing history. Excuse us for a second while we sigh and think about the good old days. Sigh ….
Back in the old days “Press any key to continue” was a cornerstone of nearly every batch file ever written. All you had to do back then was insert a pause command in the middle of the batch file; at that point, the batch file would halt and - like a faithful old dog - just sit there and wait for you to press a key on the keyboard. As soon as you pressed a key - any key - the batch file would resume again. Anyone interested in taking a walk down memory lane can try this simple little batch file to see how it all worked:
echo The batch file is complete.
Note: Save this as a .bat file, not a .vbs file.
No doubt many of you are thinking, “Wow, scripting is so much better and so much more powerful than batch files. I bet there’s a really cool way to do Press any key in a script.” To tell you the truth, we would have thought that way, too. But guess what: there isn’t a way to replicate this function in a script.
OK, there might be a goofy workaround of some kind, but off the top of our heads we couldn’t find one. The problem is that Windows Script Host isn’t designed to be an event-driven environment; in other words, WSH doesn’t know how to sit around and wait until an event (such as a key being pressed) occurs. Combine that with a limited ability to interact with the command line, and you end up with a scripting environment that can’t replicate the functionality of the pause command.
We know: that wasn’t exactly the answer you were hoping to get. But we do have a consolation prize for you. We can’t replicate the pause command, in which the computer sits and waits until you press any key on the keyboard. However, we can give you some code that will sit and wait until you press the ENTER key on the keyboard (and, yes, it has to be the ENTER key). This isn’t exactly what you wanted, but it will work:
strMessage = "Press the ENTER key to continue. "
Do While Not WScript.StdIn.AtEndOfLine
Input = WScript.StdIn.Read(1)
WScript.Echo “The script is complete.”
What this script does is display a message on screen, and then use StdIn (found only in WSH 5.6, which means this script runs only if you have WSH 5.6 installed) to wait for the user to input data from the command line. StdIn sits there until you press the ENTER key; as soon as you press ENTER, the script resumes. What if you press some key other than ENTER? No big deal: the value of the key you pressed will appear on screen, as will the values of any other keys you press. The script will simply display the value of any key you press until you finally hit ENTER. At that point, the script breaks out of the loop and continues.
In case you’re wondering, StdIn is really intended as a way for you to use the command line to enter data into a script. For example, your script might prompt you to enter a user name or a file name. That’s why it waits for you to press the ENTER key, as a way of indicating that data entry is complete. Otherwise, you might type the first letter of the user name only to see StdIn grab that and start running with it. The ENTER key is the official signal that data entry is done.
Try adding the following: MsgBox("Click OK to Exit")