Events are Easy

James Brundage | MVP June 2, 2026
Source

Events are easy. Events let you know when something happened, and respond to it if you choose. Events are incredibly useful. Why? Because they let you run what you want, when you want. Let's see how simple they are: Creating Events Events are easy to create. To make a new event, simply run: New-Event MyCustomEvent This will output an event object. If nothing subscribes to the event, the event will go in the queue We can get events with: Get-Event We can handle these events whenever we want. How about now? Subscribing to Events We can run code the millisecond something happens. To do this, we can subscribe to the event. There are two types of events we can subscribe to in PowerShell: Engine events and object events. Engine Events We can create engine events with New-Event. We can subscribe to engine events with Register-EngineEvent $subscriber = Register-EngineEvent -SourceIdentifier "Hello World" -Action { "Hello World" | Out-Host } $helloWorld = New-Event -SourceIdentifier "Hello World" You might notice a cool thing here: An event's "Source Identifier" can be whatever we want. Let's pass along a message: $subscriber = Register-EngineEvent -SourceIdentifier "Print Message" -Action { $event.MessageData | Out-Host } $printMessageEvent = New-Event -SourceIdentifier "Print Message" -MessageData "Hello World" If you run these scripts multiple times, you'll quickly notice that multiple subscriptions are allowed. The cool thing to note here is that event subscribers share data in their $event.MessageData Let's demonstrate this by counting twice. $doubleCounter = foreach ($n in 1..2) { Register-EngineEvent -SourceIdentifier "Counter" -Action { $event.MessageData.Counter++ $event.MessageData.Counter | Out-Host } }

$counterEvent = New-Event -SourceIdentifier "Counter" -MessageData @{ Counter=0 } Every time we run this block of code, we get two more subscriptions and a bunch more output. Before we clean up, let's talk about object events Object Events PowerShell is built on the .NET framework. .NET already has events all over the place. Let's start simple, with a timer:

Create a timer

$timer = [Timers.Timer]::new([Timespan]"00:00:03")

don't automatically reset (we only want to do this once)

$timer.AutoReset = $false

Subscribe to our event

$inAFew = Register-ObjectEvent -InputObject $timer -EventName Elapsed -Action { "In a few seconds" | Out-Host }

Start the timer (see a message in a few seconds)

$timer.Start() Lots of .NET types have events. To see if any object supports events, simply pipe it to Get-Member (events will be near the top). Timers are a good start. What about watching for file changes? $watcher = [IO.FileSystemWatcher]::new($pwd)

Register-ObjectEvent -InputObject $watcher -EventName Changed -Action { $changedFile = $event.SourceArgs[1].Fullpath $changedFile | Out-Host $changedFile }

'Check this out' > ./What-File-Changed.txt This is just the tip of the iceberg. There are literally millions of .NET types out there. They can all have events. And we can subscribe to these events in PowerShell Getting Subscribers Let's start to clean up a bit: To get any current subscribers, we can use Get-EventSubscriber Get-EventSubscriber To get events subscribing to a source, we can use: Get-EventSubscriber -SourceIdentifier "Hello World" If a subscriber has an .Action, we can get results of that action by piping to Receive-Job This pipeline will get any output from any subscriber with an action Get-EventSubscriber | Where-Object Action | Select-Object -ExpandProperty Action | Receive-Job -Keep Hopefully this will help make another part of event subscriptions "click": Not only can we run code in the background: we can easily get the results, too. Cleaning Up We can unsubscribe by using Unregister-Event

Unsubscribe from everything

Get-EventSubscriber | Unregister-Event While we're cleaning up, let's also take care of any events in the queue. We can do this with Remove-Event

Get all events, and remove them.

Get-Event | Remove-Event Now that we've cleaned up our runspace, let's clean up this post and review what we've learned: Events are Easy Events are Easy to create (New-Event) Events are Easy to list (Get-Event) Events are Easy to remove (Remove-Event) Events are Easy to subscribe to (Register-EngineEvent) Events are Easy on any object (Register-ObjectEvent) Events are Easy! Give them a try. Eventually, you'll find events are excellent tools of the trade.

Discussion in the ATmosphere

Loading comments...