How to Add a Timeout or Delay in a PowerShell Script

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
How to Add a Timeout or Delay in a PowerShell Script

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:

How to Add a Timeout or Delay in a PowerShell Script

You can also use the -milliseconds parameter to specify how long the resource sleeps in milliseconds.

Note

There are also aliases for the parameter such as and if you don’t want to type out the full name.

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:\>

Leave a Comment

Required fields are marked *