Skip to main content

How to Schedule a Task to Run on Startup in Batch Script

A common requirement for administrative and setup scripts is to configure a program or another script to run automatically every time the computer boots up. This is essential for background services, monitoring tools, or login scripts that need to be initialized with the system. The standard, built-in command-line tool for managing these startup tasks is the powerful schtasks.exe.

This guide will teach you how to use the schtasks /create command with the ONSTART trigger to create a new scheduled task that runs every time Windows starts. You will learn the essential parameters for this type of task, including how to run it with the necessary privileges.

The Core Command: schtasks /create

The schtasks.exe utility is the primary interface for the Task Scheduler. The /create switch is used to register a new task.

Syntax: schtasks /create /SC ONSTART /TN <TaskName> /TR <TaskRun> [Options]

This command must be run with administrator privileges because creating a system-wide startup task is a privileged operation.

The ONSTART Trigger Explained

The /SC (Schedule) parameter tells the Task Scheduler when to run the task. While DAILY or WEEKLY are common, ONSTART is a special event-based trigger.

  • /SC ONSTART: This configures the task to be triggered as soon as the computer boots up. The task will run in the background under the specified user account, often before any user has even logged in. This is ideal for system services and background processes.

Another related trigger is /SC ONLOGON, which runs the task every time a user logs on.

Basic Example: A Simple "Run on Startup" Task

This script schedules a batch file named CheckForUpdates.bat to run every time the system starts.

@ECHO OFF
REM This script MUST be run as an Administrator.

ECHO --- Scheduling a Task to Run on System Startup ---
ECHO.

schtasks /create ^
/SC ONSTART ^
/TN "My App Startup Check" ^
/TR "C:\Program Files\MyApp\CheckForUpdates.bat" ^
/RU SYSTEM ^
/F

IF %ERRORLEVEL% EQU 0 (
ECHO [SUCCESS] The scheduled task was created successfully.
) ELSE (
ECHO [FAILURE] The command failed.
)
note

The caret (^) is used for line continuation to make the command readable.

How the schtasks /create Command Works

The schtasks /create command creates a new task definition (an XML file) in the C:\Windows\System32\Tasks directory. When you specify /SC ONSTART, it configures the "trigger" for this task to be the system boot event. The Windows Task Scheduler service, which starts early in the boot process, reads this definition and launches the command specified in /TR once the system is ready.

By specifying /RU SYSTEM, we are telling the scheduler to run the task using the powerful NT AUTHORITY\SYSTEM account. This account has extensive privileges on the local machine and does not require a user to be logged in.

Common Pitfalls and How to Solve Them

Problem: "Access is denied." (Administrator Privileges)

This is the most common failure.

Solution: The script must be run from an elevated command prompt. Right-click your .bat file or cmd.exe and select "Run as administrator."

Problem: The Task Requires Administrator Rights to Run

If the script being scheduled (CheckForUpdates.bat in our example) needs to perform administrative actions itself, simply running it as SYSTEM is usually enough. However, if you run it as a normal user, you need to ensure the task itself is elevated.

Solution: Use the /RL HIGHEST switch. This tells the Task Scheduler to run the task with the highest available privileges for the user specified in /RU.

schtasks /create ... /RU MyAdminUser /RP MyPassword /RL HIGHEST

Problem: The Task Command Path Contains Spaces

The /TR (Task Run) parameter is notoriously tricky with quotes.

Solution: The most reliable method is to escape the inner quotes with backslashes.

REM This is the robust, correct syntax for a path with spaces.
/TR "\"C:\My Scripts\My Task.bat\""

You can also pass arguments to your script this way: /TR "\"C:\My Scripts\My Task.bat\" /silent"

Practical Example: A "Start Monitoring Tool at Boot" Script

This script registers a background monitoring tool to start with the system. It uses all the best practices, including running as the SYSTEM account and forcing the creation.

@ECHO OFF
SETLOCAL
TITLE Startup Task Installer
REM This script must be run as an Administrator.

SET "TaskName=System Resource Monitor"
SET "TaskCommand=C:\MonitoringTools\SysMon.exe"

ECHO --- Installing the Startup Task for the System Monitor ---
ECHO.

IF NOT EXIST "%TaskCommand%" (
ECHO [ERROR] The monitoring tool was not found at:
ECHO "%TaskCommand%"
GOTO :End
)

ECHO Scheduling '%TaskName%' to run at every system startup...
schtasks /create ^
/SC ONSTART ^
/TN "%TaskName%" ^
/TR "%TaskCommand%" ^
/RU SYSTEM ^
/F

IF %ERRORLEVEL% EQU 0 (
ECHO [SUCCESS] Task created. The monitor will run on the next reboot.
) ELSE (
ECHO [FAILURE] Failed to create the scheduled task.
)

:End
ECHO.
PAUSE
ENDLOCAL

Conclusion

The schtasks /create command is the definitive tool for programmatically creating startup tasks from a batch script.

Key takeaways for using it successfully:

  • You must run the script as an Administrator.
  • Use the /SC ONSTART trigger to have the task run when the computer boots.
  • Use /TN to give your task a descriptive name and /TR to specify the command to run.
  • For background tasks that don't require user interaction, running them as the SYSTEM account (/RU SYSTEM) is a common and powerful practice.
  • Use the /F switch to allow your script to be re-run safely, overwriting any existing task with the same name.