Updated March 6, 2023
Introduction to PowerShell pause
PowerShell Pause cmdlets are used to halt the script’s execution for a certain period, wait for the user inputs to enter and then proceed with the execution, slow down the speed of the execution, or wait for another process or job to complete first and resume execution. By above all means, Pause cmdlets halts the execution interactively or not interactively.
Syntax
Multiple cmdlets use to pause the PowerShell executions. Its keyword and syntax are as below.
a. Pause
Simple command to pause the script execution indefinitely until the Enter button is pressed.
b. Start-Sleep
Pauses the script execution for a certain amount of time. We can specify the time in Seconds or MilliSeconds. This cmdlet ships with Microsoft.PowerShell.Utility Module.
c. Read-Host
The Read-Host command reads the input from the console entered by a user. This command works interactively and needs user input every time for the script to proceed further. The read-Host cmdlet is part of Microsoft.PowerShell.Utility Module.
d.Wait-Job
Wait-Job cmdlet helps to hold the execution of the script. Generally, we use this command when we don’t want the second command to run unless the previous command executes completely. Commands are wrapped inside this block to complete the execution of that block. This command is a part of Microsoft.PowerShell.Core module.
e. Set-PSBreakPoint
Set-PSBreakPoint is a debugger program, but we can use it to pause the script execution until the ‘C’ keyword is pressed or the editor’s Continue button is pressed. We can set the Line, Script, and Variable breakpoint where the script stops for the execution.
f. Timeout
Cmdlet utility Timeout.exe can also be used in the PowerShell to pause the script execution for certain specified seconds.
g. System.Threading.Thread.Sleep
We can use the System.Threading.Thread class and its method Sleep() to halt the script or the PowerShell Execution. We can specify the milliseconds inside the method in Int32 format.
h. [Console]::ReadKey()
In this method, PowerShell waits for any key to enter by a user. Unfortunately, this method only works directly with the console. You can’t use this method inside the script.
How did PAUSE cmdlets work in PowerShell?
Although there are various syntaxes for Pause cmdlets, their main purpose is to hold the execution for a specific time or indefinitely if not input provided. Once you use those cmdlets, the debugger puts the script on hold at the current state and waits for the input; once the input provided, the debugger starts the execution from where it left.
Examples of PowerShell pause
Here are the examples mention below
Example #1 – Pause cmdlet example
In the below example, the script will wait for the user input after running the first line and continues the execution once enter is pressed, as shown in the below output table. This is interactive, and someone has to press enter when asked for it.
If the script is planned to run in the background, then we need to avoid using the Pause cmdlet.
Write-Output "Script will delete Temp files"
pause
Write-Output "Starting delete process"
Output:
Once enter pressed,
Example #2 – Start-Sleep Cmdlet example
Start-Sleep cmdlet halts the execution of the script for a certain period of the specified time. You can specify the time in seconds or milliseconds.
This cmdlet is designed to run non-interactively, so if the script is planned to run in the background, it is suitable for it.
$i=0
while($i -lt 5){
Write-Output "i = $i ....Next step is after 2 seconds"
Start-Sleep -seconds 2
$i++
}
Output:
In the above example, we have specified the Start-Sleep in seconds. You can also use the Milliseconds using -Milliseconds parameter.
Example #3 – Read-Host cmdlet Example
Although the Read-Host cmdlet is to get the input from the user and later used in the script, it is also considered for holding the PowerShell script execution. In the below example, we will ask the user’s choice for the input and where the execution waits for the input and then proceeds.
Write-Output "Printing different names"
$name = Read-Host "Enter Name to print "
Write-Output "Name = $name"
Output:
After entering the value.
Example #4 – Timeout
Timeout.exe is a cmd command, but we can leverage cmd commands in PowerShell. We need to specify the time in seconds.
Write-Output "Running script"
Timeout /T 10
Write-Output "This line will run after 10 seconds"
Output:
When you press, any key execution continues without any wait during the timeout period. If you prefer no interruption in timeout, then you can use switch /NOBREAK.
The only way is to stop timeout is to press CTRL + C.
Example #5 – Set-PSBreakPoint Example
When you set the BreakPoint at Line, variable or the script debugger halts the execution for an indefinite time, and we need to press the ‘C’ button to continue the execution. For example,
$i=0
while($i -lt 5){
$i
$i++
}
In the above example, we have set the breakpoint on Line 3.
When we press, ‘C’ execution continues. We can also set the breakpoint on variable or the script, but the main purpose is to pause the execution of the script.
Example #6 – System.Threading.Thread.Sleep
We can use the System.Threading.Thread .NET class method Sleep() to pause the execution of the script. The sleep () method accepts milliseconds.
Write-Output "Starting execution"
Write-Output "Halting execution for 5 seconds"
[System.Threading.Thread]::Sleep(5000)
Write-Output "Execution Resumes"
Output:
Example #7 – ReadKey() example
In this method, we can use the ReadKey() method of the PowerShell Console. This method is only applicable to the console and waits until any key is pressed. When we execute this method from any console (ISE or any), then the console freezes.
Write-Output "Starting execution"
Write-Output "Halting execution until key pressed"
[Console]::ReadKey()
Write-Output "Execution Resumes"
Output:
Once we press any key,
Conclusion
Pause commands are very useful for debugging, halting the thread execution, or inputting but which cmdlet to use is upon you. We can also consider Wait-Job as a pause cmdlet, but in practice, it doesn’t stop the execution, it runs the job in the background, and when the first command completes, it starts the execution of the next command.
Recommended Articles
This is a guide to PowerShell pause. Here we discuss How did PAUSE cmdlets work in PowerShell along with the Examples and Outputs. You may also have a look at the following articles to learn more –