XML generated from Export-ScheduledTask is different from XML generated from manually exporting from Windows Task Scheduler

Hello,

I’m another noob to Powershell. I’m in a new role at work, and I have the need to migrate quite a few scheduled tasks from one server to another. My approach is to write a powershell script that will export all of the tasks; and then import them into another machine. I ran into a problem when exporting. I modified/hacked a script that I found online to export and it executes successfully (see script below). The problem is that the XML that is generated from the powershell script does not match (and in fact is missing quite a few elements) from the XML that is manually generated (by right clicking Export in Scheduled Tasks). I’ve included a screenshot of Notepad++ ‘file compare’ highlighting the differences in the XML generated.

Looking at the powershell help, I’m not seeing any parameters for Export-ScheduledTask that would cause this.

I saw from this post that someone mentioned a potential issue with start-transcript and stop-transcript. I commented those out in the script, and still had the same outcome (XML doesn’t match).

This is powershell version 5.1.19041.3570

Any thoughts on why that may be and how to get Export-ScheduledTask to generate the same XML as manually exporting?

Thanks for any thoughts.

#############################################################
# Export all Tasks developed by Blah (Under the assumption that the Author in the XML of the file has the word blah)
# Kind of hackey but it works 
# Definitely important to get the $ExportedPath
# variable configured properly (note the trailing slash '\')
# This script was based on a script found here: https://powershelladministrator.com/2017/06/27/exporting-all-scheduled-tasks/
#############################################################

$LogFile = "C:\Tools\PowerShell\Task Scheduler\Logging\ExportScheduledTasks.log"
$ExportedPath = "C:\Tools\PowerShell\Task Scheduler\ExportedTasks\"


#Start-Transcript -Path $LogFile
#Write-Output "Start exporting of scheduled tasks."

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

$Tasks = Get-ScheduledTask -TaskPath "\" | ? {$_.Author -match 'blah'}
$Tasks | % {"$($_.TaskPath)"}  | Select -Unique
Foreach ($Task in $Tasks)
{
    #Write-Output "Task folder: $Task"
    $TaskName = $Task.TaskName
    #Filter out any more tasks by name
    If(($TaskName -match "User_Feed_Synchronization") -or ($TaskName -match "Optimize Start Menu Cache Files"))
    {
    }
    Else
    {
        $TaskInfo = Export-ScheduledTask -TaskName $TaskName #-TaskPath $TaskFolder
        $FilePath = "$ExportedPath$TaskName.xml"
        $TaskInfo | Out-File $FilePath
        Write-Output "Saved file $BackupPath$TaskFolder$TaskName.xml"
    }
}

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

I dont have an answer for you, but I did validate similar results as you for both PowerShell 5 and 7.

My first thought is that Export-ScheduledTask is “smart” and leaving off default values in the export. So I changed the task to disabled, and then did another export and sure enough, it was in the exported XML. Maybe this might explain the behavior?

2 Likes

Lou,
Welcome to the forum. :wave:t3:

Have you tried to import it for testing? :thinking:

If you want to switch between the GUI and the command line you may use the schtasks tool.

I confirmed schtasks.exe and export-scheduled task both consistently export the same thing, which differs slightly from the gui exported task. I would make sure you are running as admin.

I was able to reproduce, (although for me it was different elements, which is to be expected). I wonder if the code that runs in each simply processes things differently on the export, and there are certain values that are ‘default’ and if not included, will default to that.

For mine, the export powershell command added a couple elements:

<DisallowStartOnRemoteAppSession>false</DisallowStartOnRemoteAppSession>
    <UseUnifiedSchedulingEngine>false</UseUnifiedSchedulingEngine>

However, referencing docs, it looks like that’s the default setting for each so I would assume if this element was excluded, it just resorts to the default, so the net change should be neglitiglbe - it not being there and it being set to false in this case is the same. That would be my best guess.

WIth yours, it seems more weird though. For example, Enabled on your export was not set, but your manual one did have it set to enabled. For me, those settings aligned so I can’t reproduce that specific thing. Some of the other things are missing entirely from my test scheduled task which makes sense as it probably didn’t have those settings to begin with.

The other thing that might be different, is the account that is doing the export. Maybe running it as an admin versus non-admin or in different contexts will change the output.

don’t have an awesome answer just some of my own findings and thoughts :frowning:

Have you tried importing them to compare via the GUI?

So … to sum it up:

The GUI exports ALL settings and the PowerShell cmdlet only exports the non-default setting.

Again: I’d try to import the XML to see if it does the job. :man_shrugging:t3:

Wow. Thanks for all of the thoughtful responses! I’ll need some time to digest, but will definitely report back.

Sorry for the delayed follow up.

I tested locally on one machine. I have confirmed that if I have say, Task A in the Task Scheduler, and then I

  1. manually right click and export Task A (to say, ManExportTaskA.xml)
  2. then use powershell to export Task A (to say, PSExportTaskA.xml)
  3. then use powershell to import PSExportTaskA.xml,
  4. manually right click and export PSExportTaskA.xml (to say, ManPSExportTaskA.xml)
  5. compare ManExportTaskA.xml to ManPSExportTaskA.xml in Notepad++, the files match!

Again, I really appreciate everybody chiming in. All of the comments were super insightful. You have a really warm community here. Glad to be a part of it.

2 Likes