Export Scheduled Tasks Using PowerShell

Last Update: Sep 04, 2024 | Published: Jan 30, 2013

SHARE ARTICLE

When Microsoft revised the Task Scheduler in Windows Vista, one major piece they worked on the use of XML for the task definition. The benefit to you is that you can export a scheduled task and import it on a totally different machine. While the techniques I’m going to demonstrate should work for just about any scheduled task, I’m assuming you intend to use them with a custom task that you have created.
In this first article, I’ll show you specifically how to export scheduled tasks using PowerShell and the Management Console. In a follow-up article, I’ll discuss importing scheduled tasks in PowerShell.

Export a Scheduled Task from the Management Console

The easiest way to export a schedule task is to use the Task Scheduler management console. Find the task, then right-click it and select Export from the context menu.
 
Export tasks management powershell
I have a scheduled task that generates a text report using the SystemInfo.exe command line tool. I created it on my Windows 8 box and would like to add it on other computers. When prompted, I’ll save the XML file to a central location. The exported file will default to using the task name as the file name. Some task names are a bit verbose so feel free to adjust accordingly.
Note: One potential downside to this approach is that you can only export one task at a time.

Export a Scheduled Task from PowerShell

An alternative, and one that lends itself if you want to export multiple tasks, is to use Windows PowerShell. Unfortunately, this will only work on Windows 8 or Windows Server 2012 as it requires the ScheduledTasks module. The cmdlet to use is Export-ScheduledTask. At a minimum all you need to do is specify the task name.

​
PS C:> Export-ScheduledTask "Weekly System Info Report

But as you can see below, the result may not be what you expected.
Export tasks powershell
 
The cmdlet did exactly what it said it was going to do. You have to take the extra step of sending the output to a file.

​PS C:> Export-ScheduledTask "Weekly System Info Report" | out-file \chi-fp01ITMyTask.xml

If you have tasks with the same name but in different folders, then you’ll also need to use the –TaskPath parameter.
This also means it is relatively easy to export multiple tasks, especially from the same folder.

Get-ScheduledTask -TaskPath “MyCompany” | foreach { Export-ScheduledTask -TaskName $_.TaskName -TaskPath $_.TaskPath | Out-File (Join-Path “\chi-fp01IT” “$($_.TaskName).xml”) }

This command will get every scheduled task in the MyCompany task folder. Each task is exported and the result is saved to an xml file on the IT file share using the task name. From here it is very easy to backup ALL scheduled tasks.

​
Get-ScheduledTask | foreach {
Export-ScheduledTask -TaskName $_.TaskName -TaskPath $_.TaskPath |
Out-File (Join-Path "\chi-fp01IT" "$($_.TaskName).xml") -WhatIf
}

I’ve used the –Whatif parameter to test what would have happened as you can see below.
Export tasks powershell
 
Another major benefit to using PowerShell to schedule tasks is that you can export tasks from remote machines.

​
PS C:> get-scheduledtask -TaskName "*Windowsbackup" -CimSession chi-dc03  | Export-ScheduledTask | out-file \chi-fp01itDC03-WindowsBackup.xml

This will get the task that ends in WindowsBackup from CHI-DC03 and export it to an XML file in the IT share. The remote machine must be running Windows 8 or Windows Server 2012. But it isn’t too difficult to take some of my previous examples and apply them to one or more remote systems. In fact you may want to make this a scheduled task itself!

For single exports, using the Task Manager management console is the easier approach. And for older systems it is really your only choice. Although you can use the legacy SCHTASK.EXE command line tool to list the XML definition and pipe the text to a file:

​
C:> schtasks /query /tn "MyCompanyMyTask" /xml > c:workMytask.xml

But if you have Windows 8 or later, then PowerShell is an attractive feature and is by far the best approach for managing exports of multiple tasks or from multiple machines.

SHARE ARTICLE