Skip to main content

How to Create a Scheduled Task in Batch Script

Automating tasks to run on a regular schedule is the cornerstone of system administration. Whether you need to run a cleanup script every night, perform a backup every week, or launch an application when a user logs on, the Windows Task Scheduler is the engine that drives this automation. The standard, built-in command-line tool for managing scheduled tasks is the powerful schtasks.exe.

This guide will teach you how to use the schtasks /create command to create new scheduled tasks from a batch script. You will learn the essential parameters for defining the schedule, the command to be run, and the security context, giving you the power to fully automate your maintenance and operational scripts.

The Core Command: schtasks /create

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

Syntax: schtasks /create [options]

This command must be run with administrator privileges to create tasks that run under a different user or with elevated rights.

Key /create Parameters Explained

schtasks /create has many options, but these are the most essential for scripting.

ParameterNameDescriptionExample
/SC <schedule>ScheduleThe type of schedule. Common values: MINUTE, HOURLY, DAILY, WEEKLY, MONTHLY, ONLOGON, ONSTART./SC DAILY
/TN <taskname>Task NameThe name for your task. Must be in quotes if it contains spaces./TN "My Daily Backup"
/TR <taskrun>Task RunThe command to execute. The path must be fully qualified and properly quoted./TR "C:\Scripts\backup.bat"
/ST <starttime>Start TimeThe time of day to run the task, in HH:MM (24-hour) format./ST 23:30
/RU <username>Run as UserThe user account to run the task under. Use SYSTEM for the powerful NT AUTHORITY\SYSTEM account./RU SYSTEM
/RP <password>Run as PasswordThe password for the user specified in /RU./RP MyP@ssw0rd
/RL <level>Run LevelSets the elevation level. Use HIGHEST to ensure the task runs with administrator privileges./RL HIGHEST
/FForceForces the creation and overwrites an existing task with the same name./F

Basic Example: A Simple Daily Task

This script schedules a cleanup script to run every day at 1:00 AM.

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

ECHO --- Scheduling a Daily Cleanup Task ---
ECHO.

schtasks /create ^
/SC DAILY ^
/TN "Daily Temp File Cleanup" ^
/TR "C:\Maintenance\Cleanup.bat" ^
/ST 01:00 ^
/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 here for line continuation to make the long command more readable.

How the schtasks /create Command Works

The schtasks /create command communicates directly with the Task Scheduler service. It creates a new task definition, which is stored as an XML file in the C:\Windows\System32\Tasks directory. The Task Scheduler service constantly monitors this folder, loading tasks into memory and triggering them when their schedule or event condition is met.

Common Pitfalls and How to Solve Them

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

This is the most common failure. Creating a system-wide scheduled task is a privileged operation.

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 Command Path Contains Spaces

This is a critical syntax issue. The /TR parameter is very sensitive to quoting. If your command path has spaces, you need to use extra escaping.

Example of error message:

REM This will often FAIL.
/TR ""C:\My Scripts\My Task.bat""

Solution: Escape the Inner Quotes The most reliable way to handle a command with spaces is to escape the inner quotes with backslashes.

REM This is the robust, correct syntax.
/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"

Problem: Running a Task with Highest Privileges

If your task script needs to perform administrative actions, it must be configured to run with elevated rights.

Solution: Use the /RL HIGHEST switch. This tells the Task Scheduler to run the task with the highest available privileges for the specified user (/RU). When used with /RU SYSTEM, it runs with full system authority.

Practical Example: A "Self-Scheduling" Backup Script

This script does two things:

  1. It runs a backup now.
  2. It creates a scheduled task to run itself every night at 11 PM, ensuring the backup continues to happen automatically.
@ECHO OFF
SETLOCAL

REM --- Part 1: Check for a command-line argument ---
IF /I "%~1"=="/scheduled" (
ECHO This is the scheduled run. Performing backup only.
GOTO :RunBackup
)

ECHO --- This is the initial, manual run ---
ECHO.
ECHO Step 1: Running the backup now...
CALL :RunBackup

ECHO.
ECHO Step 2: Scheduling this script to run daily at 11 PM...
REM %~f0 is the full path to the current script.
SET "ScriptPath=%~f0"
schtasks /create ^
/SC DAILY ^
/TN "Nightly Document Backup" ^
/TR "\"%ScriptPath%\" /scheduled" ^
/ST 23:00 ^
/RU SYSTEM ^
/RL HIGHEST ^
/F

IF %ERRORLEVEL% EQU 0 (ECHO [SUCCESS] Task scheduled.) ELSE (ECHO [FAILURE] Could not schedule task.)
GOTO :End

:RunBackup
ECHO Backing up documents...
robocopy "%USERPROFILE%\Documents" "E:\Backups\Documents" /MIR
GOTO :EOF

:End
ECHO --- Script finished ---
ENDLOCAL

Conclusion

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

Key takeaways for using it successfully:

  • You must run the script as an Administrator for most useful tasks.
  • Use the essential parameters: /SC (Schedule), /TN (Task Name), and /TR (Task Run).
  • Pay close attention to the quoting for the /TR parameter (\"...\") if the path contains spaces.
  • Use /RU SYSTEM and /RL HIGHEST to create tasks that can perform powerful system-level maintenance.
  • Use the /F switch to allow your script to be re-run safely.