Self-Deleting Scheduled Task via PowerShell
Starting with PowerShell v4, there has been cmdlets available to create scheduled tasks instead of using the schtasks.exe program, or the wizard.
My organization is using PowerShell v5.1 and there is a lingering bug from the Windows Vista days relating to setting an “EndBoundary” in order to have a scheduled task expire. An expiration date/time is needed for the scheduled task to delete itself. But, if you try to set this using the cmdlets, you get an error message.
This post is a culmination of tidbits I found on several other blog/forum posts, and a metric crap-ton of my own trial and error. All because I wanted a scheduled task to delete itself once it was done. That doesn’t seem like much, does it? Yeah, I didn’t think so either, but it was. My pain is your gain.