Creating Scheduled Tasks From XML

by Apr 17, 2012

In a previous tip, we showed how you can export a scheduled task to an XML file. Now, it's time to see how you can re-import that XML file to re-create the scheduled task. Before you can use this tip, you need an XML definition for a task. Check out the Export-ScheduledTask function in our previous tip to create that file.

You can use this technique to clone a scheduled task to multiple machines, or you can first export a scheduled task to XML, then adjust all the advanced settings inside the XML file, and finally reimport the scheduled task from the adjusted XML file.

function Import-ScheduledTask {
    param(
    [Parameter(Mandatory=$true)]
    $Jobname,

    [Parameter(Mandatory=$true)]
    $Path,

    $ComputerName=$null
    )

    if ($ComputerName -ne $null) {
      $option = "/S $ComputerName"
    } else {
      $option = ''  
    }
    schtasks /CREATE /TN $jobname /XML $path $option
}

Twitter This Tip! ReTweet this Tip!