EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials PowerShell Tutorial PowerShell pause
 

PowerShell pause

Updated March 6, 2023

PowerShell pause

 

 

Introduction to PowerShell pause

PowerShell Pause cmdlets are used to halt the script’s execution for a certain period, wait for the user inputs to enter and then proceed with the execution, slow down the speed of the execution, or wait for another process or job to complete first and resume execution. By above all means, Pause cmdlets halts the execution interactively or not interactively.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Syntax

Multiple cmdlets use to pause the PowerShell executions. Its keyword and syntax are as below.

a. Pause

Simple command to pause the script execution indefinitely until the Enter button is pressed.

b. Start-Sleep

Pauses the script execution for a certain amount of time. We can specify the time in Seconds or MilliSeconds. This cmdlet ships with Microsoft.PowerShell.Utility Module.

c. Read-Host

The Read-Host command reads the input from the console entered by a user. This command works interactively and needs user input every time for the script to proceed further. The read-Host cmdlet is part of Microsoft.PowerShell.Utility Module.

d.Wait-Job

Wait-Job cmdlet helps to hold the execution of the script. Generally, we use this command when we don’t want the second command to run unless the previous command executes completely.  Commands are wrapped inside this block to complete the execution of that block. This command is a part of Microsoft.PowerShell.Core module.

e. Set-PSBreakPoint

Set-PSBreakPoint is a debugger program, but we can use it to pause the script execution until the ‘C’ keyword is pressed or the editor’s Continue button is pressed. We can set the Line, Script, and Variable breakpoint where the script stops for the execution.

f. Timeout

Cmdlet utility Timeout.exe can also be used in the PowerShell to pause the script execution for certain specified seconds.

g. System.Threading.Thread.Sleep

We can use the System.Threading.Thread class and its method Sleep() to halt the script or the PowerShell Execution. We can specify the milliseconds inside the method in Int32 format.

h. [Console]::ReadKey()

In this method, PowerShell waits for any key to enter by a user. Unfortunately, this method only works directly with the console. You can’t use this method inside the script.

How did PAUSE cmdlets work in PowerShell?

Although there are various syntaxes for Pause cmdlets, their main purpose is to hold the execution for a specific time or indefinitely if not input provided. Once you use those cmdlets, the debugger puts the script on hold at the current state and waits for the input; once the input provided, the debugger starts the execution from where it left.

Examples of PowerShell pause

Here are the examples mention below

Example #1 – Pause cmdlet example

In the below example, the script will wait for the user input after running the first line and continues the execution once enter is pressed, as shown in the below output table. This is interactive, and someone has to press enter when asked for it.

If the script is planned to run in the background, then we need to avoid using the Pause cmdlet.

Write-Output "Script will delete Temp files"
pause
Write-Output "Starting delete process"

Output:

PowerShell pause output 1

Once enter pressed,

PowerShell pause output 2

Example #2 – Start-Sleep Cmdlet example

Start-Sleep cmdlet halts the execution of the script for a certain period of the specified time. You can specify the time in seconds or milliseconds.

This cmdlet is designed to run non-interactively, so if the script is planned to run in the background, it is suitable for it.

$i=0
while($i -lt 5){
Write-Output "i = $i ....Next step is after 2 seconds"
Start-Sleep -seconds 2
$i++
}

Output:

PowerShell pause output 3

In the above example, we have specified the Start-Sleep in seconds. You can also use the Milliseconds using -Milliseconds parameter.

Example #3 – Read-Host cmdlet Example

Although the Read-Host cmdlet is to get the input from the user and later used in the script, it is also considered for holding the PowerShell script execution. In the below example, we will ask the user’s choice for the input and where the execution waits for the input and then proceeds.

Write-Output "Printing different names"
$name = Read-Host "Enter Name to print "
Write-Output "Name = $name"

Output:

PowerShell pause output 4

After entering the value.

PowerShell pause output 5

Example #4 –  Timeout

Timeout.exe is a cmd command, but we can leverage cmd commands in PowerShell.  We need to specify the time in seconds.

Write-Output "Running script"
Timeout /T 10
Write-Output "This line will run after 10 seconds"

Output:

PowerShell pause output 6

When you press, any key execution continues without any wait during the timeout period. If you prefer no interruption in timeout, then you can use switch /NOBREAK.

output 7

The only way is to stop timeout is to press CTRL + C.

Example #5 – Set-PSBreakPoint Example

When you set the BreakPoint at Line, variable or the script debugger halts the execution for an indefinite time, and we need to press the ‘C’ button to continue the execution. For example,

$i=0
while($i -lt 5){
$i
$i++
}

output 8

In the above example, we have set the breakpoint on Line 3.

output 9

When we press, ‘C’ execution continues. We can also set the breakpoint on variable or the script, but the main purpose is to pause the execution of the script.

output 10

Example #6 – System.Threading.Thread.Sleep

We can use the System.Threading.Thread .NET class method Sleep() to pause the execution of the script. The sleep () method accepts milliseconds.

Write-Output "Starting execution"
Write-Output "Halting execution for 5 seconds"
[System.Threading.Thread]::Sleep(5000)
Write-Output "Execution Resumes"

Output:

output 11

Example #7 – ReadKey() example

In this method, we can use the ReadKey() method of the PowerShell Console. This method is only applicable to the console and waits until any key is pressed. When we execute this method from any console (ISE or any), then the console freezes.

Write-Output "Starting execution"
Write-Output "Halting execution until key pressed"
[Console]::ReadKey()
Write-Output "Execution Resumes"

Output:

output 12

Once we press any key,

output 13

Conclusion

Pause commands are very useful for debugging, halting the thread execution, or inputting but which cmdlet to use is upon you. We can also consider Wait-Job as a pause cmdlet, but in practice, it doesn’t stop the execution, it runs the job in the background, and when the first command completes, it starts the execution of the next command.

Recommended Articles

This is a guide to PowerShell pause. Here we discuss How did PAUSE cmdlets work in PowerShell along with the Examples and Outputs. You may also have a look at the following articles to learn more –

  1. PowerShell Sleep
  2. PowerShell Escape Character
  3. PowerShell Count
  4. Windows PowerShell ISE

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW