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 theStart-Sleepcmdlet. Since PowerShell also supports CMD commands, you can also use thePauseandTimeoutcommands.
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 theStart-Sleepthe cmdlet is to suspend the activity in a script or session for a specified amount of time. You can use it with the-Secondsand-Millisecondsparameters, as shown in the examples below.
To suspend the activity for 5 secondsStart-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.3Start-Sleep -Milliseconds 4300Start-Sleep -Seconds 4 -Milliseconds 300
You can also make use of built-in PowerShell aliases. You can use sleep and -s in place ofStart-Sleepand-Secondsrespectively. So, the previous command can be written as:Sleep -s 4.3
Finally, you can press CTRL + C to break out ofStart-Sleep.
Read-Host
The purpose of theRead-Hostcmdlet 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-AsSecureStringor-MaskInputparameters at the end to display asterisks in place of the password.
Console.ReadKey
TheConsole.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
TheRawUI.ReadKeymethod 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
TheThread.Sleepmethod 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
TheSet-PSBreakpointcmdlet 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 checkingMicrosoft’s Set-PSBreakPoint documentationfor further details.
Wait-Job
you may use theWait-Jobcmdlet 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-Timeoutswitch 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 themorecommand to pause after one screen of results is displayed.
For instance, to view the first screen of info from a file namedtestfile.new, you could use:more < testfile.new
you’re able to also pipe tomoreas 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 may use thepauseandtimeoutcommands as they are compatible with Powershell.
If you just add thepausecommand to your script, you’ll receive the following or similar message when it is executed:Press Enter to continue…
you’re able to usetimeoutinstead 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’re able to use the/nobreakswitch as such:timeout /t 7 /nobreak