There can be different cases where we need to pause in PowerShell. We can pause the Powershell in order to wait for user input, slow down the execution, wait for another process or event, etc. The PowerShell provides different mechanisms in order to pause the execution.
PowerShell Pause Methods
Following methods, commands can be used to pause in PowerShell. Even some of them are not native PowerShell commands or methods they can be easily used in PowerShell.For example, MS-DOS pause and timeout commands can be used to pause the PowerShell terminal.
- Start-Sleep Command
- Thread Sleep
- Read-Host Command
- Console ReadKey
- RawUI ReadKey
- MS-DOS Pause Command
- MS-DOS Timeout Command
Pause PowerShell with Start-Sleep Command
The most popular command to pause in PowerShell is the Start-Sleep command or cmdlet. Also, the Start-Sleep command is the official and native command provided by PowerShell. The Start-Sleep command has the following syntax where the -Seconds parameter is used to specify pause seconds and -Milliseconds parameter is used to specify pause milliseconds.
In the following example we will set the pause time as 3 seconds.
Start-Sleep -Seconds 3
We can also use only milliseconds to specify the pause time.
Start-Sleep -Milliseconds 300
1 second is equal to the 1000 milliseconds. So in the following example we will set wait time as 3000 milliseconds which is equal to the 3 seconds.
Start-Sleep -Milliseconds 3000
Both the -Seconds and -Milliseconds parameter can be specified at the same time.
Start-Sleep -Seconds 3 -Milliseconds 500
Also, the seconds parameter can be used to specify both seconds and milliseconds by using a double or floating-point value. In the following example, we will pause for 3.5 seconds which is equal to the 3 seconds 500 milliseconds.
Start-Sleep -Seconds 3.5
Pause PowerShell with Thread Sleep
PowerShell is developed with the .NET framework and the .NET [System.Threading.Thread] class can be used to pause the thread itself. The Sleep() method of the Thread is used by calling [System.Threading.Thread]:Sleep(). The pause time is specified as milliseconds to the Sleep() method. In the following example, we will pause the PowerShell for 2 seconds 100 milliseconds which is also equal to the 2300 milliseconds.
Alternatively, the pause time can be specified with a TimeSpan type. The TimeSpan object is created by providing the Days, Hours, Minutes, Seconds, and Milliseconds value. In the following example, we will pause for 2 seconds 100 milliseconds.
[System.Threading.Thread].Sleep([TimeSpan]::New(0, 0, 0, 2, 100))
Pause PowerShell with Read-Host Command
The Read-Host command is used to read some input from the PowerShell command line interface. As the Read-Host command is not desgined and created to pause shell is provides very basic usage where the PowerShell is paused until the user inputs some data or press a key. Generally some message or prompt is printed to informat user.
Read-Host -Prompt "Please enter some value or press any key"
Pause PowerShell with Console ReadKey
The Console ReadKey is another method to pause PowerShell. Console ReadKey provides similar usage to the Read-Host command. The PowerShell is paused in order to get some input interactively. The Console ReadKey is executed like “[Console]::ReadKey()” method.
Pause PowerShell with RawUI ReadKey
The RawUI ReadKey is very similar to the Console ReadKey method where the pressed key can be defined and until a key is pressed the PowerShell waits.
Pause PowerShell with MS-DOS Pause Command
MS-DOS predecessor of the PowerShell. The PowerShell is created as compatible with the MS-DOS and MS-DOS ‘pause‘ command can be used to pause the PowerShell. The pause command can be used like below.
When the pause command is issued the following line printed in the PowerShell screen where resume requires pressing the Enter key.
Press Enter to continue...
Alternatively, we can execute the pause command by providing the MS-DOS or cmd as an execution environment.
cmd /c 'pause'
Pause PowerShell with MS-DOS Timeout Command
The timeout command is the native command used to pause the MS-DOS. The timeout command can be also used to pause PowerShell. The timeout command can be also used to specify the wait time in seconds by using the /t option. In the following example, we will wait for the PowerShell for 2 seconds.
timeout /t 2
Also we can provide -1 as wait time which will wait for indefinitely.
timeout /t -1