How to Run a Scheduled Task on Demand in Batch Script
Scheduled tasks are designed to run automatically based on a trigger, such as a specific time or a user logon. However, there are many situations where you need to run a task manually, outside of its normal schedule. You might need to trigger a nightly backup during the day for testing, run a cleanup script immediately to free up disk space, or kick off a maintenance task in response to an alert.
This guide will teach you how to use the standard, built-in schtasks.exe command to run any existing scheduled task on demand. You will learn the simple syntax, the importance of administrator privileges, and how to check if the command was successful.
The Core Command: schtasks /run
The schtasks.exe utility is the primary interface for the Task Scheduler. The /run switch is specifically designed to start a scheduled task immediately, ignoring its regular triggers.
Syntax: schtasks /run /TN <TaskName>
/TN <TaskName>: The Task Name of the scheduled task you want to run.- This command must be run with administrator privileges to run tasks that are configured to run with elevated rights or as a different user.
Key /run Parameters Explained
| Parameter | Name | Description | Example |
|---|---|---|---|
/TN <taskname> | Task Name | (Required) The name of the task to run. Must be in quotes if it contains spaces. The path is part of the name (e.g., \Microsoft\Windows\Defrag\ScheduledDefrag). | /TN "My Daily Backup" |
/S <computer> | Server | An optional parameter to run a task on a remote computer. | /S MyRemotePC |
Basic Example: A Simple "Run Now" Command
This script will immediately start a scheduled task named "Nightly Document Backup".
@ECHO OFF
REM This script should be run as an Administrator.
SET "TaskToRun=Nightly Document Backup"
ECHO --- Running a Scheduled Task On Demand ---
ECHO.
ECHO Attempting to run the task: "%TaskToRun%"
schtasks /run /TN "%TaskToRun%"
IF %ERRORLEVEL% EQU 0 (
ECHO [SUCCESS] The task was started successfully.
) ELSE (
ECHO [FAILURE] The command failed. Check the error message above.
)
How the Command Works
The schtasks /run command communicates with the Task Scheduler service and instructs it to start the specified task immediately. The Task Scheduler then looks up the task's definition (the command to run, the user account, etc.) and launches it in a new process.
Important: The schtasks /run command is asynchronous or non-blocking. This means that the batch script does not wait for the task to finish. The command returns success as soon as the task has been successfully started. The task itself will continue to run in the background.
Common Pitfalls and How to Solve Them
Problem: "Access is denied." (Administrator Privileges)
This is the most common failure. If the task is configured to run with elevated privileges (which many are), you need to be an administrator to start it manually.
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 system cannot find the file specified." (Wrong Task Name)
This error from schtasks means the task name you provided does not exist.
Solution: You must use the exact task name, including its folder path if it's in one. You can find the full name in the Task Scheduler GUI or by running schtasks /query.
- A root-level task is just
"My Task". - A task in a folder is
"\My Folder\My Task".
Problem: The Command Finishes, but the Task is Still Running
This is not a problem, but the expected behavior. The /run command finishes as soon as the task is launched.
Solution: If you need to wait for the task to complete, you must build a "polling" loop in your script that repeatedly checks the status of the task until it is no longer "Running."
:CheckStatus
schtasks /query /TN "My Task" | find "Running" > NUL
IF %ERRORLEVEL% EQU 0 (
ECHO The task is still running. Waiting...
TIMEOUT /T 10 > NUL
GOTO :CheckStatus
)
ECHO The task has finished.
Practical Example: A "Maintenance Menu" Script
This script provides a simple menu for a user or administrator to manually trigger common, pre-defined maintenance tasks that are set up in the Task Scheduler.
@ECHO OFF
SETLOCAL
TITLE On-Demand Task Runner
REM This script must be run as an Administrator.
:Menu
CLS
ECHO --- Manual Task Runner ---
ECHO.
ECHO 1. Run the "Nightly Backup" task
ECHO 2. Run the "Disk Cleanup" task
ECHO 3. Run the "Defrag" task
ECHO Q. Quit
ECHO.
CHOICE /C 123Q /M "Please enter your choice: "
IF %ERRORLEVEL% EQU 4 GOTO :EOF
IF %ERRORLEVEL% EQU 3 GOTO :RunDefrag
IF %ERRORLEVEL% EQU 2 GOTO :RunCleanup
IF %ERRORLEVEL% EQU 1 GOTO :RunBackup
:RunBackup
ECHO Running "Nightly Backup"...
schtasks /run /TN "Nightly Backup"
GOTO :Continue
:RunCleanup
ECHO Running "Disk Cleanup"...
schtasks /run /TN "Disk Cleanup"
GOTO :Continue
:RunDefrag
ECHO Running Windows Defrag task...
schtasks /run /TN "\Microsoft\Windows\Defrag\ScheduledDefrag"
GOTO :Continue
:Continue
ECHO.
ECHO The task has been started.
PAUSE
GOTO :Menu
Conclusion
The schtasks /run command is the definitive tool for manually starting a scheduled task from a batch script.
Key takeaways for using it successfully:
- You must run the script as an Administrator to run most system tasks.
- Use the syntax
schtasks /run /TN "<TaskName>". - You must use the full task name, including its folder path if it's not in the root.
- The command is non-blocking; it returns as soon as the task starts, not when it finishes.
By using schtasks /run, you can add a powerful layer of on-demand control to your automated environment.