Register ObjectEvent - PowerShell


The Register-ObjectEvent cmdlet is designed to facilitate advanced event handling for .NET objects in PowerShell. It empowers you to register event handlers for specific events raised by objects, enabling you to perform custom actions or execute automation tasks when these events occur. This capability is particularly valuable in scenarios where you need to monitor and respond to changes in object states or perform specific actions based on events.


Register-ObjectEvent [-InputObject] <object> [-EventName] <string> [-Action] <scriptblock> [[-Confirm] <bool>] [[-WhatIf] <bool>]


Specifies the .NET object for which you want to register event handlers.

Indicates the name of the event you want to listen for. The event name should match the name of the event raised by the object.

Defines the action to be executed when the specified event occurs. The action is provided as a script block.

Prompts for confirmation before executing the command.

Shows what would happen if the command was executed without actually executing it.


  • None.


Example 1: Registering a Click Event for a Button

$button = [Windows.Forms.Button]::new()
Register-ObjectEvent -InputObject $button -EventName Click -Action { "Button clicked!" }

Example 2: Monitoring Changes in a File’s LastWriteTime

$file = [System.IO.FileInfo]::new('test.txt')
Register-ObjectEvent -InputObject $file -EventName PropertyChanged -Action { "File's LastWriteTime changed!" }

Common Issues

  • Incorrect Event Name: Ensure that the specified EventName matches an event that the object raises. Otherwise, no event will be triggered.
  • Invalid Input Object: The InputObject parameter must be a valid .NET object that supports event handling. If the specified object doesn’t support events, the command will fail.


  • Use Register-ObjectEvent in conjunction with Wait-Event to wait for a specific event to occur within a specified timeout period.
  • Integrate the command with other PowerShell commands to automate tasks based on object events, such as logging, sending notifications, or modifying other objects.
  • Wait-Event
  • New-Event
  • Remove-Event