search
top

How To Use PowerShell to Export Scheduled Tasks

How To Use PowerShell to Export Scheduled Tasks

Introduction

We all know what a pain it is to have to recreate scheduled tasks on Windows when moving to new systems or restoring crash or corrupted systems. Making a backup of these tasks is always good to do, so why not do this with PowerShell and make things much simpler.

The Code

The script is fairly simple

  • Define a new-object
  • Connect to the object on the server
  • Get the contents of the tasks folder (C:\Windows\System32\Tasks)
  • Define outfile and outfile_temp
  • Cycle through and save as xml

Below is the output of the getmytasks.ps1 script.

Code: getmytasks.ps1

$sch = New-Object -ComObject("Schedule.Service")
$sch.Connect("<SERVERNAME>")
$tasks = $sch.GetFolder("\").GetTasks(0)

$outfile_temp = "C:\tasks\{0}.xml"

$tasks | %{
$xml = $_.Xml
$task_name = $_.Name
$outfile = $outfile_temp -f $task_name
$xml | Out-File $outfile
}

Basically the script goes to the C:\Windows\System32\Tasks directory and cycles through each task and creates an xml file for each one.

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

top