Tech News Today
  • News
  • How To
  • Buyer’s Guide
  • Hardware
    • PC Builds
    • Computer Cases
    • Motherboards
    • Peripherals
    • RAM
  • Gaming
  • Software
  • More
    • Mobile
    • Gadgets
    • Laptops
    • Reviews
Facebook Twitter Instagram
Tech News Today
  • News
  • How To
  • Buyer’s Guide
  • Hardware
    • PC Builds
    • Computer Cases
    • Motherboards
    • Peripherals
    • RAM
  • Gaming
  • Software
  • More
    • Mobile
    • Gadgets
    • Laptops
    • Reviews
Tech News Today
Home»Windows»How To Pause PowerShell (Step-By-Step Guidelines)

How To Pause PowerShell (Step-By-Step Guidelines)

AnupBy AnupMay 17, 2022
powershell pause

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.

Table of Contents

  • How to Pause PowerShell
    • Start-Sleep
    • Read-Host
    • Console.ReadKey
    • RawUI.ReadKey
    • Thread.Sleep
    • Set-PSBreakPoint
    • Wait-Job
    • More
    • Pause and Timeout Commands
  • Frequently Asked Questions
    • How to PowerShell Pause Until Keypress?
    • How to PowerShell Pause for ‘X’ Seconds?
    • How to PowerShell Pause Before Exit?
    • How to PowerShell Pause Output Screen?
    • How to PowerShell Pause for Input?

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

start-sleep powershell pause

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 Start-Sleep and -Seconds respectively. So, the previous command can be written as:
Sleep -s 4.3

sleep-s-powershell-pause

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"

read-host-mask-input

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

console-readkey

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')

rawui-readkey

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)

system-threading-thread-sleep

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.

set-psbreakpoint-powershell-pause

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

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

type-more-command-powershell-pause

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…

pause-command-powershell-cmd

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

timeout-command-powershell-cmd

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 timeout command.

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.

how-to
Anup

I used to be a travel writer. Then I took an arrow in the knee.

Related Posts

change drive letter windows 10 11

How To Change A Drive Letter On Windows 10 Or 11

June 8, 2022
disable touch screen windows 11

How to Disable the Touchscreen on Windows 11?

June 8, 2022
enable virtualization windows 11

How to Enable Virtualization on Windows 11

June 7, 2022
how-to-open-file-location-on-windows-11-1

How to Open File Location on Windows 11

June 7, 2022
hibernate windows 11

How to Enable Hibernate in Windows 11?

June 7, 2022
powershell vs cmd

PowerShell VS CMD – What’s The Difference

June 7, 2022
Add A Comment

Leave A Reply Cancel Reply

Latest Posts
fighting-games

11 Best Fighting Games on Steam

June 8, 2022
what-is-the-blue-circle-on-my-samsung

What Is the Blue Circle on My Samsung

June 8, 2022
should-i-use-wd-discovery

What Is WD Discovery? Should I Use It?

June 8, 2022
You may also like
how-to-clear-other-storage-on-iphone

How to Clear Other Storage on iPhone?

June 8, 2022
change drive letter windows 10 11

How To Change A Drive Letter On Windows 10 Or 11

June 8, 2022
how to reset imac without password

How to Reset iMac Without Password

June 8, 2022
Recommended
Cookie Clicker Garden Guide

Cookie Clicker Garden Guide to Unlocking Every Seed

September 26, 2021
monitor no signal

Computer Turns On But Monitor Says No Signal (9 Ways To Fix)

October 25, 2021
Facebook Twitter Pinterest
  • Home
  • About Us
  • Privacy Policy
  • Affiliate Disclosure
© 2022 TechNewsToday.

Type above and press Enter to search. Press Esc to cancel.