Exporting all scheduled tasks

I wanted to have an export of all scheduled tasks ready for all servers that I manage, thus I created a little powershell tool that does that exactly.

It appeared to be pretty simple to reach this goal, as there is an Export-ScheduledTask function in Powershell.

This is what I used, but I wanted to have an export of all my scheduled tasks, without the default ones from Microsoft. So I needed to filter out a little bit.
There are two lines of code to filter out the unwanted tasks. First I removed the ones in the sub folder Microsoft and OfficeSoftwareProtection. After I run trough them to save the export (xml) of the task, I check if the task name doesn’t contain the text “User_Feed_Synchronization” or “Optimize Start Menu Cache Files”, if they aren’t called that, they’ll be exported.

(Get-ScheduledTask).TaskPath | Where { ($_ -notmatch "Microsoft") -and ($_ -notmatch "OfficeSoftware") } | Select -Unique
If(($TaskName -match "User_Feed_Synchronization") -or ($TaskName -match "Optimize Start Menu Cache Files"))

Troughout the running of the tool, it’ll output information to its log file. The files are saved in subdirectory names which correspond to the scheduled task folder they are found in.

This is the complete code for the tool:
$LogFile = "D:\Data\Logging\ExportScheduledTasks.log"
$BackupPath = "D:\Data\Tasks"
$TaskFolders = (Get-ScheduledTask).TaskPath | Where { ($_ -notmatch "Microsoft") -and ($_ -notmatch "OfficeSoftware") } | Select -Unique
Start-Transcript -Path $LogFile
Write-Output "Start exporting of scheduled tasks."

If(Test-Path -Path $BackupPath)
{
Remove-Item -Path $BackupPath -Recurse -Force
}
md $BackupPath | Out-Null

Foreach ($TaskFolder in $TaskFolders)
{
Write-Output "Task folder: $TaskFolder"
If($TaskFolder -ne "\") { md $BackupPath$TaskFolder | Out-Null }
$Tasks = Get-ScheduledTask -TaskPath $TaskFolder -ErrorAction SilentlyContinue
Foreach ($Task in $Tasks)
{
$TaskName = $Task.TaskName
If(($TaskName -match "User_Feed_Synchronization") -or ($TaskName -match "Optimize Start Menu Cache Files"))
{
}
Else
{
$TaskInfo = Export-ScheduledTask -TaskName $TaskName -TaskPath $TaskFolder
$TaskInfo | Out-File "$BackupPath$TaskFolder$TaskName.xml"
Write-Output "Saved file $BackupPath$TaskFolder$TaskName.xml"
}
}
}

Write-Output "Exporting of scheduled tasks finished."
Stop-Transcript

12 thoughts on “Exporting all scheduled tasks

  1. Great script. When I run it, it is taking roughly 15 seconds per task to export. Is this normal?

    Like

    1. Yes, you are correct, it can take some time… The export-scheduled task feature in powershell isn’t the quickest one around. In my case I don’t notice as I run this trough the task schedular at night

      Like

      1. Ok cool. I thought maybe there was something wrong with my server. 😁 I will be running it as a weekly task anyway so no Biggie.

        Like

  2. Seems that this script only backups for the user that created the schedule job. How would you add to this script to backup all user created jobs?

    Like

    1. In that case your user probably isn’t an administrator. Or if you’ve created a scheduled task to run this script, make sure you select the option “run with highest privileges”.
      You can’t export tasks that the user account you’re using doesn’t have access to.

      Like

  3. Nice script.
    Small modification to get rid of “else”.
    —————
    If( -Not (($TaskName -match “User_Feed_Synchronization”) -or ($TaskName -match “Optimize Start Menu Cache Files”))) {
    $TaskInfo = Export-ScheduledTask -TaskName $TaskName -TaskPath $TaskFolder
    $TaskInfo | Out-File “$BackupPath$TaskFolder$TaskName.xml”
    Write-Output “Saved file $BackupPath$TaskFolder$TaskName.xml”
    }
    —————

    Like

Leave a comment