I'm looking for a way to automatically do some clean up tasks when the PowerShell session quits. So for example in my profile file I start a process which needs to run in the background for quite a lot of tasks and I would like to automatically close that process when I close the console.
Is there some function the PowerShell automatically calls when closing the session as it does with prompt
when displaying the prompt?
There is the Register-EngineEvent
cmdlet which you can use to attach an event handler to the Exiting
event:
Register-EngineEvent PowerShell.Exiting -Action { ... }
Note however, that this event will not be fired if you close the console window.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With