While reviewing and revising the manuscript for Windows PowerShell v2.0: TFM 3rd ed. I had the opportunity to revisit our chapter on working with events in PowerShell. An event in Windows is when something happens like a mouse-click, a process being created or window resized. In PowerShell you can easily watch for an event of interest and then do something when it happens or fires. This is referred to as an event subscription.
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
PowerShell v2.0 has a few event related cmdlets you can use for creating an event subscription: Register-WMIEvent, Register-ObjectEvent and Register-EngineEvent. Today I want to show you something I think you might find helpful using the last cmdlet in the list.
Register-EngineEvent is used to to subscribe to Powershell related events such as when PowerShell is exiting. I’ve been asked in the past if PowerShell has a shutdown script mechanism and it doesn’t. But you can create your own with this cmdlet.
The cmdlet needs the sourceid of the event and a script block that will execute when the event fires.,
PS C:\> register-engineevent PowerShell.Exiting –action {write-host "Au revoir, adios and good-bye" –foregroundcolor Green}
Run this in your PowerShell session and you should see a new event subscription.
PS C:\> Get-EventSubscriber
SubscriptionId : 1
SourceObject :
EventName :
SourceIdentifier : PowerShell.Exiting
Action : System.Management.Automation.PSEventJob
HandlerDelegate :
SupportEvent : False
ForwardEvent : False
Type Exit to close PowerShell and you should briefly see your logoff message. Are you starting to get some ideas?
Obviously you don’t want to have to recreate the event subscription every time you start PowerShell, so add it to your PowerShell profile. Here’s something I’ve been trying lately.
1: #register PowerShell closing event
2: Register-EngineEvent "PowerShell.Exiting" -action {
3: #uncomment to run the shutdown script
4: # c:\scripts\posh\psexit.ps1
5: $voice.speak(("Good-bye Jeff")) | Out-Null
6: Write-Host "Bye!!" -foregroundcolor green
7:
8: $msg="{0} Exiting PowerShell. Total Session Time {1}" -f `
9: (get-date),((get-date) - (ps -id $pid).starttime).tostring()
10:
11: $msg | Add-Content "$env:temp\pshlog.txt"
12: } | Out-Null
As you can see the Action scriptblock can be as long as you need it. I have commented out for right now a line that executes a shutdown script. I can modify this script as needed to do tasks such as exporting history, aliases or whatever and it will run whenever PowerShell exits.
I like my computer to be friendly so I have it speak to me, I use the SAPI.SPVoice object earlier in my profile to create $voice. If I have the speakers turned down, I’ll at least see a message written to the screen. The last item is creating a log file in my temp directory that simply stores the current date and time and a timespan object indicating how long my PowerShell session was running.
The entire Register-EngineEvent expression is piped to Out-Null simply to supress the output object.
Of course there are caveats. This doesn’t work with PowerShell ISE or most likely other applications that host PowerShell, although perhaps you’ll test and let me know. The event also won’t fire if you kill the PowerShell process or close the window using the window control. You must type ‘Exit’ to trigger the event which will kick off your action scriptblock.
Does this look interesting? How do you think you could take advantage of this? What types of exiting actions are you using? I hope you’ll share.
1 thought on “Powershell: Exit Stage Left”
Comments are closed.