When working with Windows Task Scheduler through the command line (schtasks.exe), you might encounter the common error:
Invalid syntax. Mandatory option 'sc' is missing
. This occurs because schtasks.exe requires explicit scheduling parameters even for on-demand tasks. To create a task that runs only when manually triggered (on-demand), you need to specify the scheduling type as ONCE with a far-future trigger date. Here's the proper syntax:schtasks /create /tn "TestTask" /tr "C:\\path\\to\\program.exe" /sc ONCE /sd 01/01/2099 /st 00:00
This creates: 1. A task named "TestTask" 2. Configured to run just once (ONCE schedule) 3. With a dummy trigger date far in the future (2099) 4. That you can manually run anytime using:schtasks /run /tn "TestTask"
For more complex tasks, you might prefer using an XML definition file:schtasks /create /tn "AdvancedTask" /xml "task_definition.xml"
Example task_definition.xml content:<?xml version="1.0" encoding="UTF-16"?> <Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task"> <RegistrationInfo> <Description>Runs my custom script on demand</Description> </RegistrationInfo> <Triggers /> <!-- Empty triggers section --> <Actions Context="Author"> <Exec> <Command>"C:\\scripts\\myscript.bat"</Command> </Exec> </Actions> </Task>
- Always run Command Prompt as Administrator when creating system tasks
- Use double quotes around paths containing spaces
- For PowerShell scripts, specify the PowerShell executable as the program and the script path as argument
- Test tasks using
schtasks /run /tn "TaskName"
before implementing in productionHere's how to create an on-demand backup task:
schtasks /create /tn "DB_Backup" /tr "powershell.exe -File C:\\scripts\\backup.ps1" /sc ONCE /sd 12/31/2099 /st 23:59
You can then trigger it via:
schtasks /run /tn "DB_Backup"
Or through PowerShell:
Start-ScheduledTask -TaskName "DB_Backup"
When attempting to create a scheduled task in Windows that runs only on-demand (without any predefined schedule), many developers encounter the error:
Invalid syntax. Mandatory option 'sc' is missing.
This occurs because
schtasks.exe
requires at least a minimal schedule configuration, even for on-demand tasks.To create a task that runs only when manually triggered, we need to:
schtasks /create /tn "MyOnDemandTask" /tr "C:\path\to\program.exe" /sc ONCE /st 00:00 /f
Key parameters:
/sc ONCE
: Sets a one-time schedule/st 00:00
: Midnight time (arbitrary for our purpose)/f
: Forces creation if task exists
For more control, use an XML definition file:
<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<RegistrationInfo>
<Description>On-demand task example</Description>
</RegistrationInfo>
<Triggers>
<RegistrationTrigger>
<Enabled>false</Enabled>
</RegistrationTrigger>
</Triggers>
<Actions Context="Author">
<Exec>
<Command>C:\path\to\program.exe</Command>
</Exec>
</Actions>
</Task>
Create task using XML:
schtasks /create /tn "MyOnDemandTask" /xml task.xml
To manually run your task:
schtasks /run /tn "MyOnDemandTask"
For modern systems, PowerShell offers better control:
$action = New-ScheduledTaskAction -Execute "C:\path\to\program.exe"
$trigger = New-ScheduledTaskTrigger -AtStartup -RandomDelay 00:00:00
Register-ScheduledTask -TaskName "MyOnDemandTask" -Action $action -Trigger $trigger -Description "On-demand task"
Disable automatic trigger:
Disable-ScheduledTask -TaskName "MyOnDemandTask"
- Windows requires at least minimal schedule configuration
- Using
/sc ONCE
with arbitrary time is the simplest solution - XML definition provides most flexibility
- PowerShell offers more modern and powerful alternatives