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 t
There is the Register-EngineEvent cmdlet which you can use to attach an event handler to the Exiting event:
Exiting
Register-EngineEvent PowerShell.Exiting -Action { ... }
Note however, that this event will not be fired if you close the console window.