9

I want to schedule a task from the command prompt (using schtasks.exe) to run once and delete the task entry from the Task Scheduler Library after completion.

After looking through the usage guide, I settled on this command:

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

...but that gives me this error (I'm running Windows 10)

ERROR: The task XML is missing a required element or attribute.
(40,4):EndBoundary:

Looking it up online, I found out about this KB article that suggests using the /V1 switch to get rid of the error but on adding /V1 I am asked for the current user password to schedule the task. I don't want that.

Is there any way to schedule a task to be executed once and then deleted from the task library after execution completes?

I've tried this, but it didn't work:

schtasks /create /TN SomeTaskName /TR "schtasks /delete /TN SomeTaskName /F" /SC ONCE /ST 12:00

A kludgy solution I came up with that works is this:

schtasks /create /TN SomeTaskName /TR "notepad.exe" /SC MINUTE /MO 1 /ST 12:00 /ET 12:02 /Z

It has the side-effect of performing the task thrice before stopping.

4 Answers 4

4

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).

1
4

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 &.

1

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
3
  • 2
    Thanks for answering but I've already mentioned in my question that I am not keen on using this switch as doing so requires me to enter my password to schedule the task.
    – Vinayak
    Dec 21, 2016 at 14:16
  • Sorry. I got reading the docs and forgot your V1 section! Given the odd XML error output from SCHTASKS compared to its usual output it looks like you have stumbled upon a bug.
    – Carl
    Dec 21, 2016 at 14:45
  • I think I solved this issue but I don't remember how. It was quite a while ago. I'll post an answer if I remember what I did to get around this.
    – Vinayak
    Dec 22, 2016 at 14:11
1

You can use create a ScheduledJob with PowerShell. At the end of the job script, you can delete the job!.

Register-ScheduledJob -RunNow -Name "Archive-Scripts3"  -ScriptBlock {
  "test" | out-file -filepath c:\temp\test3.txt
  Get-ScheduledJob -Name "Archive-Scripts3" | Unregister-ScheduledJob -Force
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .