Windows command-line programming tip: waiting a specified time or a key press
Posted: (EET/GMT+2)
Today's post is about Windows command-line batch programming and PowerShell scripting. Oftentimes, you need a way to wait for a while, such as ten seconds, before you continue doing something. For instance, if you are doing remoting, you might wish to wait for the destination computer to reboot, for example.
Now, how do you wait in these scripting environments, the "DOS box" or PowerShell? There's a simple built-in command for that in Windows: timeout. This command has been available since Windows XP I believe, but is nonetheless available in Windows Vista, 7 and 8, including the server versions of course.
This command takes the parameter /T, which specifies the timeout in seconds. For example, "timeout /t 10" would wait ten seconds. By default, the commands waits for the specified time or the user's key press, similar to the old pause command.
However, you can also use the /NOBREAK switch with the timeout command, in which case the command doesn't allow a key press to stop the waiting of the specified time. However, as always with built-in batch commands, you can hit Ctrl+C to cancel the executing of your .BAT/.CMD/.PS file.
Even so, the tiny timeout command is a valuable tool in your scripting toolbox. For more information, type in "timeout /?" to see the help page of the command.
Hope this helps!