Wait-Process PowerShell Command

Wait-Process | Taking on PowerShell one cmdlet at a time | Weekly Blog

Share this post:

This is a part of an on-going blog series written by Adam Gordon. Each week, Adam will walk you through a PowerShell command, showing you when and how to use each one. This week, Adam covers Wait-Process.

When to use Wait-Process? 

The Wait-Process cmdlet waits for one or more running processes to be stopped before accepting input.

In the PowerShell console, this cmdlet suppresses the command prompt until the processes are stopped. You can specify a process by process name or process ID (PID), or pipe a process object to Wait-Process.

Wait-Process works only on processes running on the local computer.

What version of PowerShell am I using?

Get the PowerShell Version from your machine:

$PSVersionTable

This command shows you the PowerShell version information on your machine.

How to use Wait-Process? 

Stop a process and wait:

$nid = (Get-Process notepad).id
Stop-Process -Id $nid
Wait-Process -Id $nid

This example stops the Notepad process and then waits for the process to be stopped before it continues with the next command.

The first command uses the Get-Process cmdlet to get the ID of the Notepad process. It stores the ID in the $nid variable.

The second command uses the Stop-Process cmdlet to stop the process with the ID stored in $nid.

The third command uses Wait-Process to wait until the Notepad process is stopped. It uses the Id parameter of Wait-Process to identify the process.

Wait-Process

 

Specifying a process:

$p = Get-Process notepad
Wait-Process -Id $p.id
Wait-Process -Name “notepad”
Wait-Process -InputObject $p

These commands show three different methods of specifying a process to Wait-Process.

The first command gets the Notepad process and stores it in the $p variable.

The second command uses the Id parameter, the third command uses the Name parameter, and the fourth command uses the InputObject parameter.

NOTE: These commands have the same results and can be used interchangeably.

Wait for processes for a specified time:

This command waits 30 seconds for the Notepad and Winword processes to stop. If both processes are not stopped, the cmdlet displays a non-terminating error and the command prompt.

Wait-Process -Name notepad, winword -Timeout 30

Learn last week’s command: Invoke-Item.

Need PowerShell training? Check out ITProTV’s PowerShell online IT training courses.