Wait until a particular event is raised before continuing to run.
Syntax Wait-Event [[-SourceIdentifier] string] [-Timeout int] [CommonParameters] Key: -SourceIdentifier string Wait only for events with the specified source identifier. By default, Wait-Events waits for any event. -Timeout int The maximum time, in seconds, to wait for the event to occur. The default, -1, waits indefinitely. The timing starts when you submit the Wait-Event command. If the specified time is exceeded, the wait ends and the command prompt returns, even if the event has not been raised. No error message is displayed. CommonParameters: -Verbose, -Debug, -ErrorAction, -ErrorVariable, -WarningAction, -WarningVariable, -OutBuffer -OutVariable.
Wait-Event suspends execution of a script or function until a particular event is raised. Execution resumes when the event is detected. To cancel the wait, press CTRL+C.
This feature provides an alternative to polling for an event.
The response to an event may be determined in two different ways: by using the -Action parameter of the event subscription and by waiting for an event to return and then respond with an action.
Wait for the next event that is raised:
PS C:\> wait-event
Wait for the next event that is raised with a source identifier of "ProcessStarted":
PS C:\> wait-event -sourceIdentifier "ProcessStarted"
Wait up to 90 seconds, for the next event that is raised with a source identifier of "ProcessStarted":
PS C:\> wait-event -sourceIdentifier "ProcessStarted" -timeout 90
Wait for a timer event on a timer that is set for 2000 milliseconds:
PS C:\> $timer.Interval = 2000
PS C:\> $timer.Autoreset = $false
PS C:\> $timer.Enabled = $true
PS C:\> Wait-Event Timer.Elapsed
“Don't wait for extraordinary opportunities. Seize common occasions and make them great. Weak men wait for opportunities; strong men make them” ~ Orison Swett Marden
Related PowerShell Commands:
Get-Event - Get events in the event queue
Wait-Job - Wait for a background job