Creating Scheduled Tasks From XML

Share |

Join PowerShell.com!
Subscribe to Feed

PowerShell eBook
Sign up for
Your PowerTip of the Day:

Award-winning PowerShellPlus


Admin Guide to PowerShell Remoting


Mastering PowerShell eBook

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!


Posted Apr 17 2012, 06:00 AM by ps1
Copyright 2012 PowerShell.com. All rights reserved.