Skip to main content

How to Create a Scheduled Task from an XML File in a Batch Script

While the standard schtasks /CREATE command with its switches (/SC, /ST, etc.) is great for creating simple scheduled tasks, it cannot configure advanced properties like multiple triggers, idle conditions, or specific network requirements. For full control over every possible task setting, you must use a more powerful method: defining the entire task in an XML file.

This guide will teach you the standard and most robust method for creating a complex scheduled task by importing an XML definition file using the schtasks.exe command. You will learn the essential "export-modify-import" workflow, the correct syntax, and the critical importance of running with administrator privileges.

danger

CRITICAL NOTE: Creating or modifying scheduled tasks in the system-wide Task Scheduler is a privileged operation. You must run your script with full administrator privileges.

Core Command: schtasks /CREATE /XML

The schtasks.exe utility has a special mode that bypasses all the individual switches and instead reads the complete task definition from an XML file. This gives you access to every possible task setting.

Syntax: schtasks /CREATE /TN "Task Name" /XML "Path\To\Task.xml"

  • /CREATE: The action to create a new task.
  • /TN "Task Name": The Task Name for the new task being created.
  • /XML "Path\To\Task.xml": The path to the XML file that contains the task definition.

The Essential First Step: Getting a Template with schtasks /QUERY /XML

Writing a valid task scheduler XML file from scratch is extremely difficult and error-prone. The standard and highly recommended practice is to create a similar task manually using the graphical Task Scheduler (taskschd.msc), and then export it to XML to use as a template.

Command to Export: schtasks /QUERY /TN "\My Tasks\MyTemplateTask" /XML > "MyTemplate.xml"

  • /QUERY: The action to query an existing task.
  • /XML: Specifies that the output should be in XML format.
  • > "MyTemplate.xml": Redirects the output to a new file.

This gives you a perfectly formatted and valid XML file that you can then modify for your script.

The Workflow: Export, Modify, and Import

The professional workflow for this task is a simple three-step process:

  1. Export: Create a task in the GUI that is close to what you want and export it to an XML file using schtasks /QUERY /XML.
  2. Modify (Optional): Open the XML file in a text editor. You can change the <Command>, <Arguments>, or other settings as needed. It's common to change the <Author> and <UserId> fields to make the task more generic.
  3. Import: Use schtasks /CREATE /XML in your script to create the new task from your modified XML file.

Basic Example: Cloning an Existing Task

This script demonstrates the core concept by exporting an existing task and then importing it under a new name.

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

SET "TemplateTask=\Microsoft\Windows\AppLocker\MsaClean"
SET "NewTaskName=\My Tasks\ClonedMsaClean"
SET "XmlFile=%TEMP%\task_template.xml"

ECHO --- Cloning a Scheduled Task via XML ---
ECHO.

ECHO Step 1: Exporting the template task to XML...
schtasks /QUERY /TN "%TemplateTask%" /XML > "%XmlFile%"
IF %ERRORLEVEL% NEQ 0 (ECHO Failed to export. & GOTO :EOF)

ECHO.
ECHO Step 2: Creating the new task from the XML file...
REM The /F switch will overwrite the new task if it already exists.
schtasks /CREATE /TN "%NewTaskName%" /XML "%XmlFile%" /F

IF %ERRORLEVEL% EQU 0 (
ECHO [SUCCESS] New task "%NewTaskName%" created successfully.
) ELSE (
ECHO [FAILURE] Failed to create the new task.
)

DEL "%XmlFile%"

Key schtasks Parameters Explained

  • /CREATE: The action to create.
  • /TN <TaskName>: The full path and name for the new task.
  • /XML <XmlFile>: The source XML file.
  • /F: Forces the creation and overwrites the task if it already exists. This is highly recommended for scripts to make them safely re-runnable.

Common Pitfalls and How to Solve Them

  • Administrator Rights: This is the number one cause of failure. You will receive an "Access is denied" error if your script is not run from an elevated prompt. Solution: You must run the script as an Administrator.

  • Invalid XML: The schtasks command is very strict. If there is a single syntax error in your XML file (e.g., a missing </tag>), the command will fail, often with a generic "The task XML is malformed" error. Solution: Always start with a known-good template exported from the Task Scheduler. Edit it carefully in a good text editor that has syntax highlighting (like Notepad++ or VS Code).

  • User Context in XML (<UserId>): The exported XML will contain the <UserId> and <Author> of the user who created the template (e.g., MY-PC\Admin). If you deploy this task on another machine, that user might not exist. Solution: For a generic task that should run with elevated privileges, it is a best practice to edit the XML and change <UserId>S-1-5-18</UserId> (the SID for the SYSTEM account) or <UserId>S-1-5-32-544</UserId> (the SID for the Administrators group).

Practical Example: An Installer Script for a Complex Task

This script automates the creation of a nightly cleanup task for a custom application. It dynamically creates the XML file and then registers the task. This is a common pattern for software installers.

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

SET "TaskName=\MyCoolApp\NightlyCleanup"
SET "AppPath=C:\Program Files\MyCoolApp\cleanup.exe"
SET "XmlFile=%TEMP%\cleanup_task.xml"

ECHO --- Creating a complex scheduled task ---
ECHO.

REM --- Step 1: Dynamically create the XML file using ECHO commands ---
(
ECHO ^<?xml version="1.0" encoding="UTF-16"?^>
ECHO ^<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task"^>
ECHO ^<Triggers^>
ECHO ^<CalendarTrigger^>
ECHO ^<StartBoundary^>2023-10-27T02:00:00^</StartBoundary^>
ECHO ^<ScheduleByDay^>
ECHO ^<DaysInterval^>1^</DaysInterval^>
ECHO ^</ScheduleByDay^>
ECHO ^</CalendarTrigger^>
ECHO ^</Triggers^>
ECHO ^<Principals^>
ECHO ^<Principal id="Author"^>
ECHO ^<UserId^>S-1-5-18^</UserId^>
ECHO ^<RunLevel^>HighestAvailable^</RunLevel^>
ECHO ^</Principal^>
ECHO ^</Principals^>
ECHO ^<Actions Context="Author"^>
ECHO ^<Exec^>
ECHO ^<Command^>"%AppPath%"^</Command^>
ECHO ^<Arguments^>/quiet^</Arguments^>
ECHO ^</Exec^>
ECHO ^</Actions^>
ECHO ^</Task^>
) > "%XmlFile%"

ECHO Step 2: Create the task from the generated XML...
schtasks /CREATE /TN "%TaskName%" /XML "%XmlFile%" /F

IF !ERRORLEVEL! EQU 0 (ECHO [SUCCESS] Task created.) ELSE (ECHO [FAILURE] Failed to create task.)

DEL "%XmlFile%"
ENDLOCAL
note

The ^ is used to escape the special < and > characters so they are treated as literal text by the ECHO command.

Conclusion

Using XML to define a scheduled task is the most powerful and flexible method available, giving you access to every possible configuration option.

  • The core command is schtasks /CREATE /TN "TaskName" /XML "task.xml".
  • The best practice is to never write the XML from scratch. Always create a template in the GUI and export it with schtasks /QUERY /XML.
  • Always run your script as an Administrator.
  • Pay close attention to the <UserId> tag in the XML to ensure your task runs with the correct permissions.