PowerShell – Save Scheduled Tasks

powershellscheduled-tasktask-scheduler

I want to save my scheduled tasks using powershell.

I tried this:

$taskpath = "\mytasks\"   # all of my tasks are in this folder in Task Scheduler
$savefolder = "C:\tasks"  # where I want to save the xml files

Get-ScheduledTask -TaskPath $taskpath | foreach { Export-ScheduledTask -TaskName $_.TaskName | Out-File (Join-Path $savefolder "$($_.TaskName).xml") }

Those paths exist.

But I get this error: Export-ScheduledTask : The system cannot find the file specified.

What am I doing wrong?

Best Answer

You missed to supply TaskPath into Export-ScheduledTask cmdlet:

-TaskPath [<String>]
    Specifies the path for a scheduled task in Task Scheduler namespace. You
     can use \ for the root folder. If you do not specify a path, the cmdlet
     uses the root folder.

Use

$taskpath = "\mytasks\"   # all of my tasks are in this folder in Task Scheduler
$savefolder = "C:\tasks"  # where I want to save the xml files

Get-ScheduledTask -TaskPath $taskpath | 
    Foreach-Object {
        $_.TaskName  ### debugging  output
        Export-ScheduledTask -TaskName $_.TaskName -TaskPath $_.TaskPath | 
                Out-File (Join-Path $savefolder "$($_.TaskName).xml") }

Instead of specifying particular TaskName and TaskPath parameters, you can pipe InputObject object obtained from Get-ScheduledTask into Export-ScheduledTask cmdlet as in the following code snippet:

Get-ScheduledTask -TaskPath $taskpath | 
    Foreach-Object { $_ | Export-ScheduledTask | 
        Out-File (Join-Path $savefolder "$($_.TaskName).xml") }