PowerShell 2.0: One Cmdlet at a Time 95 Register-EngineEvent
Continuing the series looking at new cmdlets available in PowerShell 2.0. This time we look at the Register-EngineEvent cmdlet.
What can I do with it?
Subscribe to events generated by the PowerShell engine or the New-Event cmdlet.
Example:
Subscribe to an event when the PowerShell session exits, and save information including the date and time out to a log file.
Register-EngineEvent PowerShell.Exiting -Action {“PowerShell exited at " + (Get-Date) | Out-File c:\log.txt -Append}
After closing the PowerShell session (by typing Exit) the date and time it was closed is written to the log file.
PowerShell engine events are a new feature in PowerShell 2.0.