July 2018
Beginner
552 pages
13h 18m
English
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 sessionRegister-EngineEvent -SourceIdentifier PowerShell.Exiting -SupportEvent -Action { Write-Host -ForegroundColor Green -BackgroundColor Black "Bye $env:USERNAME" Start-Sleep -Seconds 3}
Read now
Unlock full access