Skip to content
GEEKnodes

Windows

Windows Task Scheduler: Automate Scripts and Programs

Windows Task Scheduler to run scripts on their own: triggers, privileges, running without sign-in, schtasks and why a task does not run.

Some tasks nobody should do by hand: a nightly backup, cleaning a downloads folder or running a script when the PC starts. Windows Task Scheduler does it for you, even with no one signed in. Here you will see how to create a well-configured task, how to do it from the command line and how to find out why a task “does nothing”.

In short

  • Each task has triggers (when), actions (what), conditions and settings (when it runs or not).
  • For scripts use Create Task, not Create Basic Task: it gives access to privileges, run account and conditions.
  • If it does not run, check the last run result and enable history, which is off by default.

Open Task Scheduler

Press Windows + R, type taskschd.msc and press Enter. You can also search “Task Scheduler” in the Start menu.

The window has three areas: on the left, the Task Scheduler Library with folders; in the middle, the tasks in the selected folder; on the right, the Actions pane. Windows and many programs keep their tasks in subfolders like Microsoft\Windows. Do not touch those; create your own folder (right-click the Library > New Folder) to keep yours together.

The four parts of a task

A task in the center with four parts: triggers, actions, conditions and settings.
When, what, under which conditions and how to react to failures.
TabDefinesExample
TriggersWhen it startsEvery day at 02:00
ActionsWhat it runspowershell.exe with a script
ConditionsMachine requirementsOnly on AC power
SettingsBehavior on failureRetry, stop if too long

The General tab adds two key things: which account runs it and with what privileges.

Available triggers

  • On a schedule: once, daily, weekly or monthly, with optional repetition (for example, every 15 minutes for 12 hours).
  • At log on: of any user or a specific one.
  • At startup: before anyone signs in.
  • On idle: when the computer has been unused a while.
  • On an event: when a specific event appears in Event Viewer.
  • On workstation lock or unlock, and on remote session connect or disconnect.

A task can have several triggers; any one is enough.

Create a task for a script, step by step

Say you have a PowerShell script at C:\Scripts\copia.ps1 you want to run every night.

  1. In the right pane click Create Task (not “Create Basic Task”).
  2. Under General, give it a clear name, such as Nightly backup.
  3. Choose Run whether user is logged on or not. Saving asks for the account password.
  4. Tick Run with highest privileges if the script needs admin rights.
  5. Under Triggers, click New, choose On a schedule, Daily and the time.
  6. Under Actions, click New and fill in:
    • Program/script: powershell.exe
    • Add arguments: -NoProfile -ExecutionPolicy Bypass -File "C:\Scripts\copia.ps1"
    • Start in: C:\Scripts
  7. Under Conditions, on a laptop, untick Start the task only if the computer is on AC power if it should also work on battery.
  8. Under Settings, tick Run task as soon as possible after a scheduled start is missed. If the PC was off at 02:00, it runs when turned on.
  9. Click OK, enter the password and test with right-click > Run.

For a .bat, put the file path directly in Program/script and the folder in Start in.

Why “Start in” matters

Left empty, the task starts in a system working folder, usually C:\Windows\System32. Any relative path in the script (a config.ini, a log.txt) points there and fails. Always fill “Start in” or use absolute paths in the script.

Comparison: with Start in empty the script looks for config.ini in System32 and fails; with C:\Scripts it finds it.
Without “Start in”, relative paths point to System32.

Which account and privileges

Background task without window: drive Z does not exist while the UNC path to the NAS works.
Without a session there is no window or mapped drives: use UNC paths.
OptionWhat happensWhen to use it
Run only when user is logged onThe window is visible; needs an open sessionPrograms with an interface, user notices
Run whether user is logged on or notRuns in background, no visible windowScripts and backups
Do not store passwordNo password prompt, but local resources onlyScripts that do not touch the network
SYSTEM accountMaximum local rights, no passwordMaintenance of the machine itself
Highest privilegesRuns elevated, no UAC promptScripts requiring admin

Two surprising consequences:

  • With “whether logged on or not” you will see nothing on screen. If the script waits for a key press, it waits forever.
  • Mapped network drives (like Z:) do not exist in that context. Use UNC paths like \\NAS\Backups.

Create tasks from the command line with schtasks

schtasks lets you create, query and delete tasks from a script, handy for setting up identical machines. Run it in an administrator console.

Create a daily task at 02:00 with the SYSTEM account and elevated privileges:

schtasks /create /tn "My tasks\Nightly backup" /tr "C:\Scripts\copia.bat" /sc daily /st 02:00 /ru SYSTEM /rl highest

Other common examples:

rem Every 30 minutes
schtasks /create /tn "My tasks\Sync" /tr "C:\Scripts\sync.bat" /sc minute /mo 30

rem Monday and Friday at 18:00
schtasks /create /tn "My tasks\Report" /tr "C:\Scripts\informe.bat" /sc weekly /d MON,FRI /st 18:00

rem At system startup
schtasks /create /tn "My tasks\Startup" /tr "C:\Scripts\arranque.bat" /sc onstart /ru SYSTEM
ParameterPurpose
/tnTask name, with folder if you like
/trProgram or script to run
/scFrequency: minute, hourly, daily, weekly, monthly, once, onstart, onlogon, onidle, onevent
/moModifier: every how many minutes, hours, days…
/stStart time in HH:mm
/ruAccount that runs it
/rl highestHighest privileges

Manage existing tasks:

schtasks /query /tn "My tasks\Nightly backup" /v /fo list
schtasks /run /tn "My tasks\Nightly backup"
schtasks /change /tn "My tasks\Nightly backup" /disable
schtasks /delete /tn "My tasks\Nightly backup" /f

If the script path has spaces, you need escaped quotes inside /tr: /tr "\"C:\My scripts\copia.bat\"". It is the most common cause of schtasks tasks that cannot find the file.

PowerShell offers cmdlets, more readable for complex tasks:

$accion = New-ScheduledTaskAction -Execute "powershell.exe" -Argument '-NoProfile -ExecutionPolicy Bypass -File "C:\Scripts\copia.ps1"' -WorkingDirectory "C:\Scripts"
$disparador = New-ScheduledTaskTrigger -Daily -At 2am
Register-ScheduledTask -TaskName "Nightly backup" -TaskPath "\My tasks\" -Action $accion -Trigger $disparador -User "SYSTEM" -RunLevel Highest

History and why a task does not run

Enable history

By default the Scheduler keeps no history. In the right pane, with the Library selected, click Enable All Tasks History. From then on each task’s History tab shows when it was triggered, when the action started and how it ended.

Those events are in Event Viewer under Applications and Services Logs > Microsoft > Windows > TaskScheduler > Operational.

Read the last run result

The Last Run Result column gives a code. The most frequent:

CodeMeaningWhat to check
0x0Finished successfullyIf it still did nothing, the problem is in the script
0x1The program returned an errorScript path, “Start in”, permissions, or a program returning 1 without failing
0x2File not foundProgram path or misplaced quotes
0x41301Task is runningMay be hung waiting for input
0x41303Has not run yetTrigger not met
0x41306Task was terminatedStopped by a user or time limit

Beware 0x1: some programs return 1 without failure. Robocopy, for example, returns 1 when it copied files successfully. In those cases the task code is not enough; check the program’s own log.

PowerShell shows the same:

Get-ScheduledTaskInfo -TaskName "Nightly backup" -TaskPath "\My tasks\"

Debugging trick

Make the script write its own output to a file. Change the action to something like:

cmd.exe /c "C:\Scripts\copia.bat > C:\Scripts\copia-log.txt 2>&1"

That way you see the real error message, which you would never read in a windowless task.

Common mistakes

  • Empty “Start in” and relative paths in the script pointing to System32.
  • Laptop on battery with the AC power condition on: the task is silently skipped.
  • Mapped drive letters in a task running without session. Use UNC paths.
  • Changing the account password without updating the task: it stops with a logon error. Edit the task and re-enter it.
  • Scripts asking for confirmation or pausing: in the background they never end.
  • PowerShell script execution blocked. Pass -ExecutionPolicy Bypass in arguments instead of changing the machine policy.
  • PC off or asleep at the scheduled time and the run-ASAP option unticked.

Accessibility preferences

Quick profiles

Text and reading

Text size
Default
Line height
Default
Letter spacing
Default
Word spacing
Default

Colour

Motion and orientation

Cursor