EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials PowerShell Tutorial PowerShell pause
Secondary Sidebar
PowerShell Tutorial
  • cmdlet
    • PowerShell Wait
    • PowerShell Match
    • cmdlets in PowerShell
    • Start PowerShell from cmd
    • Add-Content in PowerShell
    • Get Help in PowerShell
    • PowerShell Copy-Item
    • PowerShell Remove-Item
    • PowerShell Move-Item
    • Get Command in PowerShell
    • PowerShell Run Command
    • Windows PowerShell ISE
    • Windows Powershell Commands
    • WinRM PowerShell
    • PowerShell Date
    • Powershell Write-Host
    • PowerShell Get-ChildItem
    • PowerShell Sort-Object
    • PowerShell Where Object
    • PowerShell Set-Content
    • PowerShell Set-Location
    • PowerShell Invoke-Command
    • PowerShell Invoke-Webrequest
    • PowerShell Get-Location
    • PowerShell Get-Date
    • PowerShell Get-Service
    • PowerShell Test-Path
    • Powershell Module Path
    • PowerShell Out-File
    • PowerShell if File Exists
    • Powershell Copy File
    • PowerShell Delete File
    • PowerShell New-Item
    • PowerShell Rename-Item
    • PowerShell ComputerName
    • PowerShell Get-Content
    • PowerShell Get-Item
    • PowerShell Get-ADUser
    • PowerShell Grep
    • PowerShell Concatenate String
    • PowerShell Get-Process
    • PowerShell Count
    • PowerShell pause
  • Basics
    • PowerShell Restart Service
    • PowerShell comment
    • PowerShell Map Network Drive
    • PowerShell Append to File
    • PowerShell print
    • What is PowerShell
    • Uses Of Powershell
    • PowerShell Empire
    • PowerShell Parameter
    • PowerShell Stop Service
    • PowerShell Versions
    • How To Install PowerShell
    • PowerShell uninstall module
    • How to Use PowerShell?
    • PowerShell Logging
    • PowerShell Tools
    • PowerShell Commands
    • PowerShell Version Command
    • PowerShell Administrator
    • PowerShell Modules
    • PowerShell Registry
    • PowerShell block Comment
    • PowerShell Verbs
    • PowerShell list
    • PowerShell add user to group
    • PowerShell Write to Console
    • Variable in PowerShell
    • PowerShell New Line
    • PowerShell prompt for input
    • PowerShell File Extension
    • Powershell Remotesigned
    • PowerShell Write to File
    • PowerShell Ping
    • PowerShell wget
    • PowerShell Global variable
    • PowerShell Get-ADGroup
    • Array in PowerShell
    • PowerShell Multidimensional Array
    • PowerShell Array of Strings
    • PowerShell? join array
    • Useful PowerShell Scripts
    • String in PowerShell
    • PowerShell Switch Statement
    • PowerShell Function Parameters
    • PowerShell vs PowerShell ISE
    • PowerShell test-connection
    • PowerShell Test-NetConnection
    • PowerShell GUI
    • PowerShell Variable in String
    • PowerShell Active Directory
  • Variables
    • PowerShell Variables
    • PowerShell Environment Variables
    • PowerShell set environment variable
    • Hashtable in PowerShell
    • Set Variable in PowerShell
  • Operators
    • PowerShell Operators
    • Comparison Operators in PowerShell
    • Logical Operators in PowerShell
    • PowerShell Boolean
    • PowerShell Like Operator
  • Control Statements
    • If Statement in PowerShell
    • If Else in PowerShell
    • Else If in PowerShell
    • Loops in PowerShell
    • For loop in PowerShell
    • PowerShell While Loop
    • PowerShell do while
    • PowerShell Loop through Array
    • PowerShell add to array
    • PowerShell ForEach Loop
    • PowerShell Break
    • PowerShell Continue
    • Switch Case in PowerShell
    • PowerShell If-Not
    • Try-catch in PowerShell
  • Functions
    • PowerShell Functions
    • PowerShell String Functions
    • powershell nslookup
    • PowerShell here string
    • PowerShell Wildcards
    • Regex in PowerShell
    • PowerShell not like
    • PowerShell Filter
    • PowerShell Sleep
    • PowerShell where
    • PowerShell join string
    • PowerShell Exit
    • PowerShell null
    • PowerShell Dictionary
    • PowerShell Location
    • PowerShell Start-Service
    • PowerShell is not digitally signed
    • PowerShell Uptime
    • PowerShell Create Directory
    • PowerShell Trim
    • PowerShell Join-Path
    • PowerShell Execution Policy
    • PowerShell SubString
    • PowerShell Format Table
    • PowerShell Import Module
    • PowerShell ForEach Object
    • PowerShell Alias
    • PowerShell Scheduled Task
    • PowerShell Convert String to Date
    • PowerShell Split String
    • PowerShell Multiline String
    • PowerShell MultiLine Comment
    • PowerShell Rename Folder
    • PowerShell Delete Folder
    • PowerShell String Replace
    • PowerShell join
    • PowerShell xcopy
    • PowerShell Base64
    • PowerShell Tail
    • PowerShell User List
    • PowerShell remove User from group
    • PowerShell JSON Format
    • PowerShell Send Mail
    • PowerShell Convert to String
    • PowerShell Start-Process
    • PowerShell change directory
    • PowerShell Open File
    • PowerShell Batch File
    • PowerShell ZIP
    • PowerShell unzip
    • PowerShell XML
    • PowerShell XML Parsing
    • Remote PowerShell
    • PowerShell Escape Character
    • PowerShell scriptblock
    • PowerShell Executable Location
    • PowerShell Import-CSV?
    • PowerShell Export CSV
  • Interview Questions
    • PowerShell Interview Questions

Related Courses

Shell Scripting Course

All in One Data Science Courses

Data Visualization Courses

PowerShell pause

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.

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.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (86,408 ratings)

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
Popular Course in this category
PowerShell Training (2 Courses, 1 Project)
  2 Online Courses |  1 Hands-on Project |   4+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Shell Scripting Training (4 Courses, 1 Project)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
Data Visualization Training (15 Courses, 5+ Projects)4.7
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

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

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

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

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more