- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to use Wait-Process in Powershell?
Wait-Process cmdlet in PowerShell is used to wait for the process to stop before the execution moves on to the next step.
Example
We have a snipping tool application running and we need to wait for the process to stop first and then move on to the next step.
PS C:\> Get-Process SnippingTool | Select Name,Id,CPU Name Id CPU ---- -- --- SnippingTool 7440 2.0625
To wait for the process to stop first we will use the Wait-Process command. You can provide ProcessName or ID.
Write-Output "Waiting for the Process to Stop" Wait-Process -Name 'SnippingTool' Write-Output 'Snipping tool Process is stopped'
Output
PS C:\> C:\Temp\TestPS1.ps1 Waiting for the Process to Stop
Once the process is stopped,
PS C:\> C:\Temp\TestPS1.ps1 Waiting for the Process to Stop Snipping tool Process is stopped
If the desired process can’t stop, the execution waits forever. You can also provide the timeout parameter in seconds so if the process can’t stop in the mentioned time, it will throw an error.
Example
Write-Output "Waiting for the Process to Stop" Wait-Process -Name 'SnippingTool' -Timeout 5 Write-Output 'Snipping tool Process is stopped'
Output
PS C:\> C:\Temp\TestPS1.ps1 Waiting for the Process to Stop CloseError: (System.Diagnostics.…cess (SnippingTool):Process) [Wait-Process], TimeoutException Snipping tool Process is stopped
In such instance we can use the change the ErrorAction setting to Stop.
Example
Write-Output "Waiting for the Process to Stop" Wait-Process -Name 'SnippingTool' -Timeout 5 -ErrorAction Stop Write-Output 'Snipping tool Process is stopped'
Output
PS C:\> C:\Temp\TestPS1.ps1 Waiting for the Process to Stop CloseError: (System.Diagnostics.…cess (SnippingTool):Process) [Wait-Process], TimeoutException
- Related Articles
- How to use –Wait parameter in Restart-Computer cmdlet in PowerShell?
- PowerShell: Wait for the First command to finish
- What is the use of Get-Process command in PowerShell?
- How to use stopwatch in PowerShell?
- How to confirm before stopping the process in PowerShell?
- What is the use of Passthru parameter in Stop-Process in PowerShell?
- How to get process output in GridView format in PowerShell ?
- How to use PowerShell as a Calculator? Or How to use PowerShell for arithmetic operations?
- How to use Measure-Object in PowerShell?
- How to use function Alias in PowerShell?
- How to use a transcript in PowerShell?
- How to use Compare-Object in PowerShell?
- How to use Hashtable splatting in PowerShell?
- How to use Array Splatting in PowerShell?
- How to stop all instances of the process in PowerShell?

Advertisements