schtasks: Scheduled Tasks
A batch file becomes really useful when it runs by itself: a backup every night, a cleanup every hour, a report every Monday, a sync at logon. On Windows that is the job of the Task Scheduler, the counterpart of Linux's cron and systemd timers. You can click tasks together in taskschd.msc, but schtasks.exe does the same from the command line, which means it can be scripted, documented and repeated on many machines.
trigger (time, logon, boot, event)
|
v
Task Scheduler service ---> action: program + arguments + "start in" folder
| runs as: you, SYSTEM, or another account
v
history: Last Run Time, Last Result (exit code)Creating a task
The essential options of schtasks /create:
| Option | Meaning |
|---|---|
/tn "Folder\Name" |
task name; a backslash puts it in a folder of the Task Scheduler Library |
/tr "program args" |
what to run |
/sc |
schedule type: minute, hourly, daily, weekly, monthly, once, onstart, onlogon, onidle, onevent |
/mo n |
modifier: every n minutes/hours/days/weeks (/sc minute /mo 15) |
/d MON,FRI |
days for weekly (or day of month for monthly) |
/st 02:30 |
start time, 24-hour HH:mm |
/sd, /ed |
start and end date |
/ru SYSTEM |
run as another account (SYSTEM needs an elevated prompt); /rp its password |
/rl highest |
run with full administrator rights (if the account has them) |
/f |
replace a task with the same name instead of asking |
C:\Users\ada>schtasks /create /tn "TAD\NightlyBackup" /tr "C:\Scripts\backup.bat" /sc daily /st 02:30
SUCCESS: The scheduled task "TAD\NightlyBackup" has successfully been created.
C:\Users\ada>schtasks /create /tn "TAD\CleanTemp" /tr "C:\Scripts\clean.bat" /sc minute /mo 30
SUCCESS: The scheduled task "TAD\CleanTemp" has successfully been created.
C:\Users\ada>schtasks /create /tn "TAD\WeeklyReport" /tr "C:\Scripts\report.bat" /sc weekly /d MON /st 08:00
SUCCESS: The scheduled task "TAD\WeeklyReport" has successfully been created.
C:\Users\ada>schtasks /create /tn "TAD\NightlyBackup" /tr "C:\Scripts\backup.bat" /sc daily /st 02:30
WARNING: The task name "TAD\NightlyBackup" already exists. Do you want to replace it (Y/N)? nSample session. Tasks created this way run as you and only while you're logged on. For a server-style job that runs whether anyone is logged on or not, use /ru SYSTEM from an elevated prompt (or a service account with /ru and /rp).
If the program path contains spaces, the quotes for it must be escaped inside /tr:
schtasks /create /tn "TAD\Sync" /sc onlogon /tr "\"C:\My Scripts\sync.bat\" --quiet"Querying, running, changing, deleting
C:\Users\ada>schtasks /query /tn "TAD\NightlyBackup"
Folder: \TAD
TaskName Next Run Time Status
======================================== ====================== ===============
NightlyBackup 10/18/2026 2:30:00 AM Ready
C:\Users\ada>schtasks /run /tn "TAD\NightlyBackup"
SUCCESS: Attempted to run the scheduled task "TAD\NightlyBackup".
C:\Users\ada>schtasks /query /tn "TAD\NightlyBackup" /fo list /v | findstr /c:"Last Run" /c:"Last Result" /c:"Task To Run"
Last Run Time: 10/17/2026 3:14:52 PM
Last Result: 0
Task To Run: C:\Scripts\backup.bat
C:\Users\ada>schtasks /change /tn "TAD\NightlyBackup" /st 03:00
SUCCESS: The parameters of scheduled task "TAD\NightlyBackup" have been changed.
C:\Users\ada>schtasks /change /tn "TAD\CleanTemp" /disable
SUCCESS: The parameters of scheduled task "TAD\CleanTemp" have been changed.
C:\Users\ada>schtasks /delete /tn "TAD\WeeklyReport" /f
SUCCESS: The scheduled task "TAD\WeeklyReport" was successfully deleted.Sample session (dates are shown in your regional format).
| Command | Does |
|---|---|
schtasks /query |
list all tasks (/fo list /v for every detail, /fo csv /nh for scripts) |
schtasks /run /tn X |
start it now (doesn't wait for it to finish) |
schtasks /end /tn X |
stop a running instance |
schtasks /change /tn X ... |
change time, program, account, /enable or /disable |
schtasks /delete /tn X /f |
delete without confirmation |
Status is Ready (waiting for its trigger), Running or Disabled. Last Result is the exit code of the last run: 0 means success, 1 usually means your batch file ended with an error, and the scheduler's own codes are large numbers: 267011 (0x41303) "has not yet run", 267009 (0x41301) "is currently running", 2147942402 (0x80070002) "file not found".
Reading the task list in batch
For scripts, /fo csv /nh gives one line per task: "TaskName","Next Run Time","Status". The playground can't reach a Task Scheduler, so here the CSV text is saved to a file first; on a real PC use ('schtasks /query /fo csv /nh') as the source:
@echo off
setlocal enabledelayedexpansion
(
echo "\TAD\CleanTemp","N/A","Disabled"
echo "\TAD\NightlyBackup","10/18/2026 3:00:00 AM","Ready"
echo "\Microsoft\Windows\Defrag\ScheduledDefrag","N/A","Ready"
echo "\TAD\Sync","N/A","Running"
) > tasks.csv
echo Our tasks:
for /f "tokens=1-3 delims=," %%a in (tasks.csv) do (
set "name=%%~a"
if /i "!name:~0,5!"=="\TAD\" (
set "name=!name:~5! "
echo !name:~0,14! %%~c next: %%~b
)
)
for /f "delims=," %%a in ('findstr /c:"\"Disabled\"" tasks.csv') do echo Warning: %%~a is disabledOur tasks:
CleanTemp Disabled next: N/A
NightlyBackup Ready next: 10/18/2026 3:00:00 AM
Sync Running next: N/A
Warning: \TAD\CleanTemp is disabled
delims=, is safe here because neither task names nor dates contain commas, and %%~a removes the quotes. !name:~0,5! takes the first five characters to keep only the tasks in the \TAD\ folder, and !name:~5! cuts that prefix off.
Writing a batch file that runs well unattended
A scheduled batch file runs in a different world from your interactive window:
| Interactive window | Scheduled task |
|---|---|
current folder = where you cd'd |
current folder = C:\Windows\System32 (unless "Start in" is set) |
| you see the output | nobody sees the output |
| you notice errors | only the exit code (Last Result) is recorded |
your mapped drives (Z:) exist |
mapped drives usually don't; use \\server\share paths |
So unattended scripts follow three rules: cd /d "%~dp0" first (work relative to the script's own folder), write a log, and exit /b with a meaningful code. Here is such a script, run three times in a row the way the scheduler would run it; the log shows the history:
@echo off
mkdir C:\tad\jobs 2>nul
(
echo @echo off
echo setlocal
echo cd /d "%%~dp0"
echo set /a run=1
echo if exist job.log for /f %%%%n in ^('type job.log'^) do set /a run+=1
echo if not exist inbox mkdir inbox
echo set /a files=0
echo for %%%%f in ^(inbox\*.txt^) do set /a files+=1
echo if %%files%%==0 ^(
echo ^>^>job.log echo run %%run%%: nothing to do
echo exit /b 0
echo ^)
echo ^>^>job.log echo run %%run%%: processed %%files%% file^(s^)
echo del /q inbox\*.txt
echo exit /b 0
) > C:\tad\jobs\job.bat
call C:\tad\jobs\job.bat
echo exit code %errorlevel%
echo report > C:\tad\jobs\inbox\a.txt
echo report > C:\tad\jobs\inbox\b.txt
call C:\tad\jobs\job.bat
call C:\tad\jobs\job.bat
echo --- C:\tad\jobs\job.log ---
type C:\tad\jobs\job.logexit code 0
--- C:\tad\jobs\job.log ---
run 1: nothing to do
run 2: processed 2 file(s)
run 3: nothing to do
The outer script writes job.bat with echo (that's why every % is doubled and (, ), > are escaped with ^) and then calls it from a different folder: thanks to cd /d "%~dp0", the job still finds its inbox and job.log. In real life you'd simply save job.bat in an editor and register it with schtasks /create /tn "TAD\Inbox" /tr "C:\tad\jobs\job.bat" /sc minute /mo 10. On a real system you would also put a timestamp (%date% %time%) in each log line.
cron, schtasks and PowerShell side by side
| Task | cron (Linux) | schtasks | PowerShell |
|---|---|---|---|
| every 15 min | */15 * * * * |
/sc minute /mo 15 |
New-ScheduledTaskTrigger -Once -At 0:00 -RepetitionInterval (New-TimeSpan -Minutes 15) |
| daily 02:30 | 30 2 * * * |
/sc daily /st 02:30 |
New-ScheduledTaskTrigger -Daily -At 2:30am |
| Mondays 08:00 | 0 8 * * 1 |
/sc weekly /d MON /st 08:00 |
New-ScheduledTaskTrigger -Weekly -DaysOfWeek Monday -At 8am |
| at boot | @reboot |
/sc onstart |
New-ScheduledTaskTrigger -AtStartup |
| list | crontab -l |
schtasks /query |
Get-ScheduledTask |
| run now | (none) | schtasks /run /tn X |
Start-ScheduledTask -TaskName X |
In PowerShell a task is assembled from a trigger and an action, then registered:
PS C:\Users\ada> $action = New-ScheduledTaskAction -Execute 'C:\Scripts\backup.bat'
PS C:\Users\ada> $trigger = New-ScheduledTaskTrigger -Daily -At 2:30am
PS C:\Users\ada> Register-ScheduledTask -TaskName 'NightlyBackup' -TaskPath '\TAD\' -Action $action -Trigger $trigger
TaskPath TaskName State
-------- -------- -----
\TAD\ NightlyBackup ReadySample output. More in PowerShell Automation and, for Linux, cron & systemd.
Test yourself
Which command runs C:\Scripts\clean.bat every 30 minutes?
schtasks /create /tn Clean /tr C:\Scripts\clean.bat /sc minute /mo 30schtasks /create /tn Clean /tr C:\Scripts\clean.bat /every 30mschtasks /run /tn Clean /sc 30schtasks /create /tn Clean /sc hourly /st 00:30
A batch file works when you double-click it but fails as a scheduled task because it can't find config.ini next to it. What's the usual fix?
- Add
cd /d "%~dp0"at the top of the script - Run the task with /rl highest
- Rename it to .cmd
- Add
pauseat the end
schtasks /query /fo list /v shows Last Result: 0. What does it mean?
- The task has never run
- The last run ended with exit code 0, i.e. success
- The task is disabled
- The task is still running