Cmdlets

Choose a cmdlet from the list on the left or search for a specific cmdlet. Choose a cmdlet from the list or search for a specific cmdlet.
About Help  Providers
 

Wait-Event

Wait-Event

microsoft.powershell.commands.utility.dll

Synopsis

Waits until a particular event is raised before continuing to run.

Syntax

Wait-Event [-SourceIdentifier] [-Timeout] [<CommonParameters>]

Detailed Description

The Wait-Event cmdlet 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. It also allows you to determine the response to an event 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.

Parameters

-SourceIdentifier <String>

Waits only for events with the specified source identifier. By default, Wait-Events waits for any event.

Aliases

None

Required?

false

Position

1

Default value

All events

Accept pipeline input?

true (ByPropertyName)

Accept wildcard characters?

false

-Timeout <Int32>

Determines the maximum time, in seconds, that Wait-Event waits 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.

Aliases

None

Required?

false

Position

named

Default value

-1

Accept pipeline input?

false

Accept wildcard characters?

false

Input Type

System.String

Return Type

System.Management.Automation.PSEventArgs

Notes

Events, event subscriptions, and the event queue exist only in the current session. If you close the current session, the event queue is discarded and the event subscription is canceled.

Examples

-------------------------- EXAMPLE 1 --------------------------

This command waits for the next event that is raised.

PS C:\>wait-event

-------------------------- EXAMPLE 2 --------------------------

This command waits for the next event that is raised and that has a source identifier of "ProcessStarted".

PS C:\>wait-event -sourceIdentifier "ProcessStarted"

-------------------------- EXAMPLE 3 --------------------------

This command uses the Wait-Event cmdlet to 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; Wait-Event Timer.Elapsed

# After 2 seconds

EventIdentifier  : 12
Sender           : System.Timers.Timer
SourceEventArgs  : System.Timers.ElapsedEventArgs
SourceArgs       : {System.Timers.Timer, System.Timers.ElapsedEventArgs}
SourceIdentifier : Timer.Elapsed
TimeGenerated    : 6/10/2008 3:24:18 PM
MessageData      :
ForwardEvent     : False

-------------------------- EXAMPLE 4 --------------------------

This command waits up to 90 seconds for the next event that is raised and that has a source identifier of "ProcessStarted". If the specified time expires, the wait ends.

PS C:\>wait-event -sourceIdentifier "ProcessStarted" -timeout 90

Online Version
Get-Event
Get-EventSubscriber
New-Event
Register-EngineEvent
Register-ObjectEvent
Register-WmiEvent
Remove-Event
Unregister-Event
Wait-Event