Taking on PowerShell one cmdlet at a time

Share this post:This blog post is part of an ongoing series by Adam Gordon. Adam will show you how to use each PowerShell command each week. This week Adam will be covering Wait-Process.

When should you use the Wait-Process?
Wait-Process cmdlet waits until one or more processes are stopped before accepting input.
This cmdlet stops the PowerShell console’s command prompt until the processes are terminated. You can specify a specific process by name or process ID (PID), and pipe a process object into Wait-Process.
Wait-Process only works for processes that are running on the local machine.
What version of PowerShell do I use?
Get the PowerShell Version for your machine
This command displays the PowerShell version information for your machine.

How to use the Wait-Process
Stop the process and wait
$nid = (Get-Process notepad).idStop-Process -Id $nidWait-Process -Id $nid
This example stops Notepad and waits for it to stop before continuing with the next command.
The Get-Process cmdlet is used to obtain the Notepad process ID. It stores the ID in $nid variable.
The Stop-Process cmdlet is used to stop the process using the ID stored in $nid.
Wait-Process is used to wait until the Notepad process stops. It uses Wait-Process’ Id parameter to identify the process.

Specifying a process
$p = Get-Process notepadWait-Process -Id $p.idWait-Process -Name “notepad”Wait-Process -InputObject $p
These commands demonstrate three methods of specifying a Wait-Process process.
The Notepad process is started by the first command. It is then stored in the $p variable.
The second command uses Id, the third uses Name, and the fourth uses InputObject.
NOTE: These commands can be used interchangeably and produce the same results.

Wait for processes to complete within a given time frame:
The command waits for 30 seconds for Winword and Notepad to stop. If neither process is stopped, the cmdlet displays an error message and prompts for further action.
Wait-Process –Name notepad, winword. -Timeout 30

Learn the command for last week: Invoke-Item.
Do you need PowerShell training? ITProTV offers PowerShell online IT training courses.