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
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.
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
-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 milliseconds
Start-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
-Seconds respectively. So, the previous command can be written as:
Sleep -s 4.3
Finally, you can press CTRL + C to break out of
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
-MaskInput parameters at the end to display asterisks in place of the password.
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 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:
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
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 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.
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.
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
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
Frequently Asked Questions
How to PowerShell Pause Until Keypress?
You can use the
ReadKey methods or the
Timeout command to pause until a keystroke is registered. The details regarding these are mentioned in the article above.
How to PowerShell Pause for ‘X’ Seconds?
Start-Sleep is the native Powershell cmdlet used for pausing a PowerShell session for the specified number of seconds. Alternatively, you can also use the
How to PowerShell Pause Before Exit?
Console.ReadKey() is commonly used to pause Powershell and display a prompt such as Press Enter to Exit. When the user presses enter, the program will exit.
How to PowerShell Pause Output Screen?
You can use the
More command to pause the output screen in PowerShell. We’ve included further details on this in the article above.
How to PowerShell Pause for Input?
This depends on the exact type of input but the
Read-Host cmdlet is most often used for this purpose. Although one could argue that
Set-PSBreakPoint and the
ReadKey methods both lead to certain forms of input as well.