Wait Process - PowerShell


Overview

Wait-Process is a PowerShell command that enables users to pause a running script or command until a specified process or set of processes complete execution. It’s particularly useful for coordinating tasks between multiple processes or ensuring that a subsequent task relies on the successful completion of a previous one.

Syntax

Wait-Process [-Name] <ProcessName> [-Timeout] <Number_Of_Seconds> [-InputObject] <ProcessInfo[]> [-Passthru] [-ErrorAction] <Stop|Continue|SilentlyContinue>

Options/Flags

  • -Name : Specifies the name of the process or processes to wait for. Can be a string or an array of strings.
  • -Timeout <Number_Of_Seconds>: Sets a timeout period in seconds for the command to wait for the process to complete. Defaults to 300 (5 minutes).
  • -InputObject <ProcessInfo[]>: Accepts an array of ProcessInfo objects as input, allowing you to specify multiple processes to monitor.
  • -Passthru: When used, returns the input process objects, regardless of their completion status.
  • -ErrorAction <Stop|Continue|SilentlyContinue>: Controls the action taken in case of errors during waiting. Defaults to “SilentlyContinue”.

Examples

Simple Usage: Wait for a single process named “notepad.exe” to complete execution.

Wait-Process notepad.exe

Multiple Processes: Wait for multiple processes to finish, using an array of process names.

Wait-Process -Name (@("notepad.exe", "wordpad.exe"))

With Timeout: Set a timeout period of 1 minute (60 seconds) for the process to complete.

Wait-Process -Name notepad.exe -Timeout 60

Passthru Mode: Return the process objects, even if they have not yet completed.

$processes = Wait-Process -Name notepad.exe -Passthru

Common Issues

  • Process Not Found: Ensure that the process name specified actually exists and is running.
  • Timeout Expired: If the process takes longer than the specified timeout to complete, the command will throw an error. Adjust the timeout value accordingly.
  • Access Denied: Ensure you have sufficient permissions to monitor the process.

Integration

With “Start-Process”: Combine with Start-Process to wait for a newly started process to finish.

Start-Process notepad.exe -Wait

In Scripting: Use Wait-Process in scripts to ensure that subsequent tasks are not executed until specific processes complete successfully.

  • Get-Process: Retrieve information about running processes.
  • Stop-Process: Stop a running process.
  • Suspend-Process: Suspend a running process.
  • Resume-Process: Resume a suspended process.