Tech News Today
  • Hardware
    • Motherboards
    • CPUs
    • Graphic Cards
    • RAM
    • SSDs
    • Computer Cases
    • Monitors
    • Peripherals
    • Power Supply Unit
    • PC Builds
    • Computer Tips
  • Software
  • Operating System
    • Windows
    • Mac
    • Linux
  • Gaming
  • Mobile
  • Console
  • More
    • Internet
    • Networking
    • Security
    • Buyer’s Guide
    • Gadgets
    • Laptops
    • Reviews
    • How To
    • News
Facebook Twitter Instagram
Tech News Today
  • Hardware
    • Motherboards
    • CPUs
    • Graphic Cards
    • RAM
    • SSDs
    • Computer Cases
    • Monitors
    • Peripherals
    • Power Supply Unit
    • PC Builds
    • Computer Tips
  • Software
  • Operating System
    • Windows
    • Mac
    • Linux
  • Gaming
  • Mobile
  • Console
  • More
    • Internet
    • Networking
    • Security
    • Buyer’s Guide
    • Gadgets
    • Laptops
    • Reviews
    • How To
    • News
Tech News Today
Home»Windows»How To Pause PowerShell (Step-By-Step Guidelines)

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

Anup ThapaBy Anup ThapaDecember 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

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
how-to
Anup Thapa
  • LinkedIn

Anup Thapa is a tech writer at TechNewsToday. He mostly writes informative articles, tutorials, and troubleshooting guides related to Windows systems, networking, and computer hardware. Anup has been writing professionally for almost 5 years, and tinkering with PCs for much longer. His love for all things tech started when he got his first PC over 15 years ago. It was a Pentium IV system running Windows XP on a single 256 MB stick. He spent his formative years glued to this PC, troubleshooting any hardware or software problems he encountered by himself. Professionally, Anup has had brief forays into a variety of fields like coding, hardware installation, writing, etc. In doing so, he's worked with people of different backgrounds and skill levels, from average joes to industry leaders and experts. This has given him not just a versatile skillset, but also a unique perspective for writing that enables him to concisely communicate complex information and solve his reader's problems efficiently. You can contact him at anup@technewstoday.com

Related Posts

amd-software-not-opening

How to Fix AMD Software Not Opening

March 3, 2023
how to create a folder

7 Ways to Create a Folder on Windows

March 3, 2023
turn off automatic updates windows

How to Turn Off Automatic Updates on Windows

March 2, 2023
Memory-Compression

What is Memory Compression in Windows? Should You Enable or Disable It

March 1, 2023
dism-vs-sfc-vs-chdsk

DISM, SFC, CHKDSK: What’s the Difference

February 28, 2023
how-to-open-exe-file

4 Ways to Open EXE File

March 2, 2023
Add A Comment

Leave A Reply Cancel Reply

Latest Posts
Memory-Compression

What is Memory Compression in Windows? Should You Enable or Disable It

March 1, 2023
dism-vs-sfc-vs-chdsk

DISM, SFC, CHKDSK: What’s the Difference

February 28, 2023
bios-settings-for-gaming

Best BIOS Settings for Gaming

February 16, 2023
You may also like
how-to-clean-hp-printer-rollers

How to Clean HP Printer Rollers

March 3, 2023
keyboard input lag

9 Ways to Fix Keyboard Input Lag

March 3, 2023
keyboard key is stuck

How to Fix a Stuck Key on a Keyboard

March 3, 2023
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)

November 10, 2022
Facebook Twitter Pinterest
  • Home
  • About Us
  • Our Team
  • Editorial Guidelines
  • Privacy Policy
  • Affiliate Disclosure
© 2023 TechNewsToday, editor@technewstoday.com | Tech Central Pvt. Ltd.

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