How do you schedule a task (using schtasks.exe) to run once and delete itself?

Solution 1:

You may want to consider using Powershell since you're on Windows 10. I believe you can use Unregister-ScheduledTask -TaskName applog -Confirm:$false to delete a scheduled task without a confirmation prompt.

Someone else has reported a similar problem, to which a user recommended adding -Compatibility V1 (although I'm not sure if this still yields a user credential prompt).

Solution 2:

Microsoft suggests adding /V1 to your command to work-around this issue.

https://support.microsoft.com/en-gb/kb/2004151

This would give you:

schtasks /create /TN SomeTaskName /TR "notepad.exe" /SC ONCE /ST 12:00 /V1 /Z

Solution 3:

The option /z does not work in conjunction with the start type once. If you use the option /v1 the command executes without errors, but the task is not deleted after the next logon/execution.

To delete a task, Windows requires an expiration date, which is not possible with once.

You may delete the task 'manually', e.g. using something like this:

... /TR "cmd.exe /c notepad.exe && schtasks /delete /TN SomeTaskName /f" ...

This command executes notepad.exe, waits for its completition and if successfull deletes the task SomeTaskName. If you want to delete the task even in case of an error, just use a single &.