So I have been working a lot on a custom PowerShell profile that has a lot of custom built in functionality for day to day use. My goal being to use the powershell console as my one stop shop for everything I do on a daily basis (with in reason of course). This would include anything from using PowerShell as an app launcher to setting up notifications, performing status checks, navigating Windows, and of course interfacing with our SQL Servers.
Along the journey of building this all powerful profile I found the need to use background jobs quite frequently. I will get it into it later what I am using them for in another post about PowerShell and Windows Toast Notifications but for the time being I’ll stick to the background jobs.
One of the major annoyances of using PowerShell Jobs was cleaning up these background jobs as they were always left behind. So my solution which I have seen some others mention was to use a Registered Object Event, and this solution worked out great.
Below is a quick Gist I created to show an example of how you can create a Job in PowerShell and automatically clean it up with a Registered Event. The Gist also shows how to have the Registered clean itself up.
Please feel free to reach out, ask questions, make random comments. I would love to get your opinion, and how you have/would solve this issue.