2

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?

3

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") }
| improve this answer | |
  • Thanks that works! Do you mind explaining why my version fails but yours works? I don't understand because the only difference is you supply the object into the loop, but my one infers the current object. I would expect my way to work as well? – lonix Jun 8 '18 at 8:05
  • 1
    @LotPings I beg your pardon for my misunderstanding double negation in English (it's different in my native tongue). – JosefZ Jun 8 '18 at 8:07
  • 1
    Forget what he said, I understood you perfectly, and thanks for helping me! I hope one day I can speak PowerShell as well as you speak English :) – lonix Jun 8 '18 at 8:11
  • @Ionix In your version Out-file receives the xml data through the pipe no longer the ScheduledTask object, so there is no TaskName Property. – LotPings Jun 8 '18 at 8:16
  • 1
    @JosefZ Sorry for being to picky on semantics. I'm no native English speaker myself and there was no harm intended. Found the fault also but was to slow to answer. One +1 is from me. – LotPings Jun 8 '18 at 8:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.