Engine events

Engine events are particular to the PowerShell engine, and can be used to trigger actions accordingly. The following example hooks into the exit event of PowerShell to display a little message to the user. Before there was PSReadLine, this was also a popular place to export the history, so that it would persist between sessions.

The following code sample shows an engine event that is fired when PowerShell closes. The sample code will simply wish you goodbye:

# In PowerShell CLI, the following engine event is triggered when closing the session
Register-EngineEvent -SourceIdentifier PowerShell.Exiting -SupportEvent -Action {
Write-Host -ForegroundColor Green -BackgroundColor Black "Bye $env:USERNAME"
Start-Sleep -Seconds 3
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.182.179