You may be trying to write a script that requires user input to complete, or sometimes the script requires a delay to let another process finish executing first.
The most used command for purposes like this is the Start-Sleep
cmdlet. Since PowerShell also supports CMD commands, you can also use the Pause
and Timeout
commands.
In this article, we’ve explained the usage of these and more commands to pause in Powershell.
How to Pause PowerShell
You can use the following methods to pause in PowerShell. As we’ve included both native and non-native methods, you can use them as appropriate.
Start-Sleep
The purpose of the Start-Sleep
the cmdlet is to suspend the activity in a script or session for a specified amount of time. You can use it with the -Seconds
and -Milliseconds
parameters, as shown in the examples below.
To suspend the activity for 5 seconds Start-Sleep -Seconds 5
To suspend the activity for 200 millisecondsStart-Sleep -Milliseconds 200
To suspend the activity for 4.3 seconds, you can use any of the following:Start-Sleep -Seconds 4.3
Start-Sleep -Milliseconds 4300
Start-Sleep -Seconds 4 -Milliseconds 300
You can also make use of built-in PowerShell aliases. You can use sleep and -s in place of Start-Sleep
and -Seconds
respectively. So, the previous command can be written as:Sleep -s 4.3
Finally, you can press CTRL + C to break out of Start-Sleep
.
Read-Host
The purpose of the Read-Host
cmdlet is to read some input from the console, and in doing so, it pauses the script. It’s normally used to prompt the user to enter data such as passwords, usernames, etc.
For instance, the command below prompts the user to enter their name, and the input is saved in the “$Name variable.”$Name = Read-Host "Enter Your Name"
If you use this to prompt the user for their password, you may want to add the -AsSecureString
or -MaskInput
parameters at the end to display asterisks in place of the password.
Console.ReadKey
The Console.ReadKey()
method is often used to pause program execution until the user presses a key. For instance, if you’re trying to pause the program until the user presses Enter, you would use it as such:Console.ReadKey().Key != ConsoleKey.Enter
RawUI.ReadKey
The RawUI.ReadKey
method is similar to the Console ReadKey method, except you can use a few more options with this method. These options are:
AllowCtrlC
– Allow CTRL + C to be processed as a keystroke instead of causing a break event.IncludeKeyDown
– Include key down events (fired when a key is pressed).IncludeKeyUp
– Include key up events (fired when the user releases a key on the keyboard).NoEcho
– Don’t display the character for the key in the window when pressed.
Note that either one of IncludeKeyDown or IncludeKeyUp, or both, must be included. With that said, you can use this method with the options as such:$host.UI.RawUI.ReadKey(‘AllowCtrlC,IncludeKeyDown')
Thread.Sleep
The Thread.Sleep
method suspends the current thread for a specified period of time. You can use this method with a millisecondsTimeout argument or a TimeSpan object, as shown below.
In the following example, the current thread is paused for 3.5 seconds[System.Threading.Thread].Sleep(3500)
Alternatively, you can input the value in the form of Days, Hours, Minutes, Seconds, and Milliseconds as shown below:[System.Threading.Thread].Sleep([TimeSpan]::New(0, 0, 0, 3, 500))
Set-PSBreakPoint
The Set-PSBreakpoint
cmdlet is used for debugging PowerShell scripts. It’s used to set breakpoints where PowerShell temporarily stops executing and hands over control to the debugger.
You can set three types of breakpoints with this cmdlet; Line breakpoint, Command breakpoint, and Variable breakpoint. The use cases for this are countless, so we recommend checking Microsoft’s Set-PSBreakPoint documentation for further details.
Wait-Job
You can use the Wait-Job
cmdlet to wait for a specified job or all jobs to be in a terminating state (completed, failed, stopped, suspended, or disconnected) before continuing execution.
In the following example, the cmdlet will pause the PowerShell script until Job1 is in a terminating state.Wait-Job -Name "Job1"
To do the same for all jobs, you could use:Get-Job | Wait-Job
You can also use the -Timeout
switch to specify in seconds how long to wait for the job to finish before a timeout occurs and the script resumes executing.
More
In case you’re running a command or script that displays a large amount of output, you can use the more
command to pause after one screen of results is displayed.
For instance, to view the first screen of info from a file named testfile.new
, you could use:more < testfile.new
You can also pipe to more
as such:type testfile.new | more
Pause and Timeout Commands
It’s worth mentioning that if you want to use CMD commands instead of Powershell cmdlets, you can use the pause
and timeout
commands as they are compatible with Powershell.
If you just add the pause
command to your script, you’ll receive the following or similar message when it is executed:
Press Enter to continue…
You can use timeout
instead if you want to specify the amount of time (in seconds) to pause for before the session continues as such:timeout /t 7
Users may sometimes unintentionally press a key which would abruptly end the timeout. If you’d prefer to ignore such keystrokes, you can use the /nobreak
switch as such:timeout /t 7 /nobreak