Table of Contents
Delay running in PowerShell scripting
If you are writing a PowerShell script file and you don’t want to continue until somebody presses a key, you can do it really easy with the timeout command.
For example, you want to open multiple URLs at once using a PowerShell script, and you want to add a delay of some seconds between each URL because they open all at once which freezes up the machine.
Here is the script without timeout parameters:
Start-Process http://google.com Start-Process https://yahoo.com Start-Process https://bing.com
Delay x seconds before running the next command
To do it, you need to add Start-Sleep command into your script. In the below example, after the first one is opened, the script will wait for 10 seconds before starting the next one.
Start-Process http://google.com Start-Sleep -Seconds 10 Start-Process https://yahoo.com Start-Sleep -Seconds 10 Start-Process https://bing.com
The output when you run the PowerShell script:
You can also use the -milliseconds parameter to specify how long the resource sleeps in milliseconds.
Start-Sleep -Milliseconds 25
Press any key before the script continues
Another option is to use the Read-Host cmdlet. This is waiting until the user provides any input before the script continues and with that allows you to easily pause a PowerShell script until a key press, for example:
PS C:\> Start-Process http://google.com PS C:\> Read-Host -Prompt "Press any key to continue..." Press any key to continue...: PS C:\> Start-Process https://yahoo.com PS C:\> Read-Host -Prompt "Press any key to continue..." Press any key to continue...: PS C:\> Start-Process https://bing.com PS C:\>