Skip to main content

How to Export a Scheduled Task to XML in a Batch Script

Exporting a scheduled task to an XML file is a critical administrative function for backup, migration, and deployment. An XML definition file is a complete, human-readable snapshot of a task, capturing every trigger, action, condition, and setting. This file can be checked into version control, used to migrate a task to another server, or serve as a definitive backup before you make changes.

This guide will teach you how to use the built-in schtasks.exe command-line utility to export an existing scheduled task to an XML file. You will learn the correct syntax and the importance of knowing the full path of the task you wish to export.

The Core Command: schtasks /QUERY /XML

The schtasks.exe utility is the primary tool for managing scheduled tasks. To export a task, you use the /QUERY action combined with the /XML format switch. This tells schtasks to display the complete definition of a specific task in XML format. We then use standard output redirection (>) to save this text to a file.

The Syntax: schtasks /QUERY /TN "Task Name" /XML > "OutputFile.xml"

  • /QUERY: The action to query or display an existing task.
  • /TN "Task Name": The Task Name. This is the full path of the task in the Task Scheduler library (e.g., "\My Tasks\Daily Backup").
  • /XML: Specifies that the output format should be XML.
  • > "OutputFile.xml": Redirects the output from the screen to the specified file.

The Essential Prerequisite: Finding the Full Task Name

Before you can export a task, you must know its exact name and folder path within the Task Scheduler. The /QUERY switch is also used for this.

Command to find a task: schtasks /QUERY /FO LIST | findstr /I "Task Name to Find"

Let's find a task related to system maintenance.

C:\> schtasks /QUERY /FO LIST | findstr /I "Maintenance"
TaskName: \Microsoft\Windows\DiskCleanup\SilentCleanup

This tells us the full task name is \Microsoft\Windows\DiskCleanup\SilentCleanup.

Basic Example: Exporting a Task

This script exports the SilentCleanup task we found earlier to an XML file on the desktop.

@ECHO OFF
REM This script is best run as an Administrator to access all tasks.

SET "TaskName=\Microsoft\Windows\DiskCleanup\SilentCleanup"
SET "OutputFile=%USERPROFILE%\Desktop\SilentCleanup_Task.xml"

ECHO --- Exporting Scheduled Task to XML ---
ECHO Task: %TaskName%
ECHO File: "%OutputFile%"
ECHO.

schtasks /QUERY /TN "%TaskName%" /XML > "%OutputFile%"

IF %ERRORLEVEL% EQU 0 (
ECHO [SUCCESS] Task exported successfully.
) ELSE (
ECHO [FAILURE] An error occurred. Check the task name and permissions.
)

How to Read the XML Output

The resulting XML file is a standard text file that can be opened in any text editor (like Notepad, VS Code, etc.). It contains a structured, detailed definition of the task.

Abridged XML Example (SilentCleanup_Task.xml):

<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.4" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<RegistrationInfo>
<Author>Microsoft Corporation</Author>
...
</RegistrationInfo>
<Triggers>
<CalendarTrigger>
<StartBoundary>2021-06-25T00:00:00</StartBoundary>
...
</CalendarTrigger>
</Triggers>
<Principals>
<Principal id="Author">
<UserId>S-1-5-18</UserId> <!-- SYSTEM Account -->
<RunLevel>LeastPrivilege</RunLevel>
</Principal>
</Principals>
<Settings>
...
</Settings>
<Actions Context="Author">
<Exec>
<Command>%windir%\system32\cleanmgr.exe</Command>
<Arguments>/autoclean /d %SystemDrive%</Arguments>
</Exec>
</Actions>
</Task>

This file can now be used with schtasks /CREATE /XML to recreate the exact same task on another machine.

Common Pitfalls and How to Solve Them

  • "ERROR: The specified task name ... does not exist.": This is the most common failure. It means the path provided with /TN is incorrect.

    • Solution: Use schtasks /QUERY to find the exact, full path to the task, including any leading backslashes and subfolders.
  • Administrator Rights: While you can often export tasks that you created as a standard user, you cannot query tasks in system-protected folders (like \Microsoft\Windows) or tasks that run as the SYSTEM user without elevated privileges.

    • Solution: For a complete backup or migration, you must run the script as an Administrator.

Practical Example: A Script to Back Up All Custom Tasks

This is a powerful administrative script. It queries all tasks on the system and uses a FOR loop to export each one to a separate XML file in a backup directory. It intelligently skips the built-in Microsoft tasks to only back up custom or third-party tasks.

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

SET "BackupFolder=%~dp0Task_Backups"
MKDIR "%BackupFolder%" 2>NUL

ECHO --- Backing Up All Non-Microsoft Scheduled Tasks ---
ECHO Backups will be saved to: "%BackupFolder%"
ECHO.

REM Get a list of all task names, then filter out the Microsoft ones.
FOR /F "tokens=1" %%T IN ('schtasks /QUERY /FO TABLE /NH ^| findstr /V /I /B "\Microsoft"') DO (

REM Create a valid filename from the task name by replacing '\' with '_'.
SET "TaskName=%%T"
SET "FileName=!TaskName:\=_!"

ECHO Backing up task: "!TaskName!"

REM Export the task using its full name.
schtasks /QUERY /TN "!TaskName!" /XML > "%BackupFolder%\!FileName!.xml"
)

ECHO.
ECHO --- Backup Complete ---
ENDLOCAL
note

This script requires DelayedExpansion (!Var!) to handle the changing variable names inside the loop.

Conclusion

The schtasks /QUERY /XML command is the definitive and most robust method for backing up and documenting scheduled tasks.

For successful and reliable exports:

  1. Always run your script as an Administrator to access all tasks.
  2. Use schtasks /QUERY first to get the exact, full name of the task you want to export.
  3. Use the command syntax schtasks /QUERY /TN "TaskName" /XML > "OutputFile.xml".

The resulting XML file provides a perfect, portable definition of your task that can be used for migration, deployment, or disaster recovery.