This will register a listener for the "MyEvent" event on the $eventsource object. Whenever that event fires, the action script block will be executed, printing a message.
Some tips for event listeners:
The $eventsource can be any PowerShell object that emits events, like a timer, filesystem watcher, etc.
The -SourceIdentifier parameter gives a unique name for your listener. Use this to unregister the listener later with Unregister-Event.
The -Action is the code block that will execute when the event fires. You can access the event data in the $EventArgs variable.
Events will continue firing even if the script that registered the listener ends. To clean up, use Unregister-Event.
You can have multiple listeners for the same event. They will all execute in order when the event fires.
For example, here's a simple filesystem watcher with an event listener:
This will watch for file changes in C:\folder and print a message whenever a file is modified. The event listener will continue even after the while loop ends, until you call Unregister-Event "FileWatcher".