How To Add Pause to a Powershell Script (With Examples)

Jason Barrett Jason Barrett | | Misc

In this article I will show you how to add a pause to a powershell script.

There are many different ways that you can add a pause or wait command, Below I will show you these ways with examples.

powershell start sleep

How To Add Pause to a Powershell Script

To Add Pause to a Powershell Script follow these steps

  1. Open PowerShell ISE via the start menu
  2. Then open the powershell script you wish to add the pause command to
  3. Add the line start-sleep -Seconds 5 (Replace the number 5 with the amount of seconds you want the script to pause for) Where you want the pause to happen
    start sleep in powershell script
  4. In the example above I added the wait command on line 3
  5. Save the powershell script
  6. Run the powershell script

Syntax for the Start-Sleep powershell command are

Start-Sleep -seconds
Start-Sleep -milliseconds

Examples

Start-Sleep -seconds 1.5  (Sleep for 1.5 Seconds)
Start-Sleep -seconds 5  (Sleep for 5 Seconds)
Start-Sleep -seconds 15  (Sleep for 15 Seconds)

Start-Sleep -milliseconds 1.5  (Sleep for 1.5 milliseconds)
Start-Sleep -milliseconds 5  (Sleep for 5 milliseconds)
Start-Sleep -milliseconds 15  (Sleep for 15 milliseconds)

How To Add Pause With Countdown to a Powershell

In this section I will show you how you can add a pause to your powershell scrip with a countdown display.  When the pause is counting down you can interrupt the count down by pressing any key and the powershell script will continue to run.

To Add Pause With Countdown to a Powershell follow these steps

  1. Open PowerShell ISE via the start menu
  2. Then open the powershell script you wish to add the pause command to
  3. Add the line timeout /t 15 (Replace the number 15 with the amount of seconds you want the script to pause for) Where you want the pause to happen
    powershell sleep with countdown
  4. Save the powershell script
  5. Run the powershell script

Below is what the timeout command will look like.

powershell timeout example

Alternative Method

By Replacing the above timeout /t 15 with the following code

1..60 | ForEach {
Start-Sleep -s 1
Write-Progress -activity “Timer Started: ” -Status $_
}

It will display a countdown timer in a slightly different way as shown below.

The above script will countdown for 60 seconds every 1 second.  Replace 60 with the amount of time you want to count down

powershell timer started

How To Add Pause With a Popup Message to a Powershell Script

With this method we will add a pause command to the powershell script and display the pause time in a popup window.

To Add Pause With a Popup Message to a Powershell Script follow these steps

  1. Open PowerShell ISE via the start menu
  2. Then open the powershell script you wish to add the pause command to
  3. Add the code from this article
  4. Save the powershell script
  5. Run the powershell script

Below is what the timeout command will look like.

powershell sleep command with popup

 

How To Make Powershell Sleep With Progress Bar

This is my personal favourite, Making powershell sleep whilst displaying a progress bar.

To Make Powershell Sleep With Progress Bar follow these steps.

  1. Open PowerShell ISE via the start menu
  2. Then open the powershell script you wish to add the pause command to
  3. Add the code below in bold
    For ($i=0; $i -le 100; $i++) {
    Start-Sleep -Milliseconds 20
    Write-Progress -Activity “Sleeping For 20 Seconds” -Status “StatusString” -PercentComplete $i -CurrentOperation “CurrentOperationString”
    }
  4. Replace the number 20 with the amount of seconds you want your powershell script to sleep for
  5. You can also replace the text “Sleeping For 20 Seconds”  With what ever you wish
  6. Save the powershell script
  7. Run the powershell script

Below is what the progress bar will look like.

powershell sleep command with progress bar