Why Use schtasks?
The graphical Windows Task Scheduler is a powerful tool for automating backups, running maintenance scripts, or launching programs at startup. However, if you are a system administrator deploying a script across 50 different servers, or if you are managing a Windows Core server without a graphical user interface (GUI), clicking through the Task Scheduler menus is impossible.
The schtasks command provides total control over the Windows Task Scheduler directly from the Command Prompt or PowerShell. You can create, delete, query, and forcefully run scheduled tasks entirely via code.
Step 1: Querying Existing Tasks
To see how schtasks works, start by listing all the tasks currently scheduled on your system. Open an Administrator Command Prompt and run:
schtasks /query
This will output a massive list of every task registered on the OS. Because the output is so large, it is usually better to format it as a list (/FO LIST) or pipe it into a text file for review:
schtasks /query /fo LIST > C:\temp\tasks.txt
Step 2: Creating a Basic Scheduled Task
The true power of schtasks is the /create parameter. When creating a task, you must specify a Task Name (/tn), a trigger schedule (/sc), a time (/st), and the actual program or script to run (/tr).
For example, assume you have a backup script located at C:\scripts\backup.bat, and you want it to run every single day at 2:00 AM. You would run:
schtasks /create /tn "DailyBackup" /tr "C:\scripts\backup.bat" /sc daily /st 02:00
The command will output “SUCCESS: The scheduled task ‘DailyBackup’ has successfully been created.”
Step 3: Advanced Scheduling Triggers
The /sc (schedule) modifier accepts several different frequencies beyond just “daily”.
- Hourly: To run a script every hour on the hour:
schtasks /create /tn "HourlySync" /tr "C:\scripts\sync.bat" /sc hourly - On Logon: To run a program the exact moment a specific user logs into the computer (useful for mapping network drives):
schtasks /create /tn "MapDrives" /tr "C:\scripts\map.bat" /sc onlogon - Weekly on Specific Days: To run a disk defragmentation script only on Friday evenings at 8:00 PM, use the
/d(day) flag:
schtasks /create /tn "FridayDefrag" /tr "C:\scripts\defrag.bat" /sc weekly /d FRI /st 20:00
Step 4: Running Tasks as the System Account
By default, tasks created via the command line will run under the permissions of the user who executed the command, and they often require that user to be physically logged in to trigger.
If you are scheduling a server maintenance script, it must run even if no one is logged into the machine. To do this, you assign the task to the highest-privileged built-in account: NT AUTHORITY\SYSTEM.
Use the /ru (Run User) flag:
schtasks /create /tn "SystemBackup" /tr "C:\scripts\backup.bat" /sc daily /st 03:00 /ru "SYSTEM"
Step 5: Deleting or Forcing a Task to Run
If you need to test your newly created backup script without waiting until 3:00 AM, you can force the task to execute immediately using the /run parameter:
schtasks /run /tn "SystemBackup"
Finally, if a script is obsolete and you want to remove the scheduled task permanently, use the /delete parameter. Append the /f (force) flag to bypass the confirmation prompt, which is essential if you are deleting tasks via an automated script:
schtasks /delete /tn "SystemBackup" /f