SchTasks.exe to create a task folder

22,726

Solution 1

few trials and solved the problem; the key is using "\" in the task name. Sample schtask.exe command line,

schtasks /create /xml "MyTask.xml" /tn "My Task Folder\My New Task"

creates a new task folder My Task Folder and creates a new task My New Task under the new folder

If the task needs to get created under an existing folder, try

schtasks /create /xml "MyTask.xml" /tn "Existing Task Folder\My New Task"

creates a new task My New Task under an existing task folder Existing Task Folder

Solution 2

There doesn't appear to be any way to do this via SchTasks.exe. If you run SchTasks.exe /Create /? at a command prompt, it shows you the available options. Creating a folder for the task doesn't show up as one of them, as far as I can see.

You might be able to do this via the ITaskScheduler interface. See this question for a discussion of the difference, and a link to a library that encapsulates the interface. (I haven't seen the library and don't know anything about it; it just appears as the solution based on the accepted answer to the linked question.)

Solution 3

It's an old thread but I found no answer elsewhere so I wrote a little powershell script which copies the task to a new folder and rewrites the UserId if wanted. Don't forget to delete the old tasks manually.

Get-ScheduledTask | ? {$_.Taskpath -ieq "\FROM"} | % {
    $oTask = $_
    [XML]$TaskXML = Export-ScheduledTask -TaskName $oTask.TaskName

    #$TaskXML.GetElementsByTagName("UserId")[0].InnerText="SYSTEM"

    Register-ScheduledTask -TaskName $oTask.TaskName -TaskPath "\TO" -Xml $TaskXML.InnerXML
}

Solution 4

A bloke named Régis Lainé wrote a killer script over at TechNet Gallery. I'm just going to put it here in case the site gets taken down.

Function Move-ScheduledTask {
    $SelectedItems = Get-ScheduledTask | Sort-Object -Property TaskName | Select-Object -Property TaskName, TaskPath, State | Out-GridView -Title "Select Tasks To Move" -OutputMode Multiple
    if ($SelectedItems -ne $null)
    {
        $TargetFolder = Get-ChildItem -Path "C:\Windows\System32\tasks" -Force -Recurse -ErrorAction SilentlyContinue | Select-Object -Property Name -Unique | Out-GridView -Title "Select Target Folder" -OutputMode Single;
        if ($TargetFolder -ne $null)
        {
            foreach ($item in $SelectedItems)
            {
                try
                {
                    Write-Host ("About to Move " + $item.TaskName + " : ") -NoNewline;
                    $SelectedScheduledTask = Get-ScheduledTask -TaskName $item.TaskName -TaskPath $item.TaskPath ;
                    Register-ScheduledTask -Xml ($SelectedScheduledTask | Export-ScheduledTask) -TaskName $SelectedScheduledTask.TaskName -TaskPath $TargetFolder.Name -ErrorAction Stop | Out-Null;
                    $SelectedScheduledTask | Unregister-ScheduledTask -Confirm:$false;
                    Write-Host ("Success") -ForegroundColor Green;
                }
                catch
                {
                    Write-Host ("Error when processing : " + $item.TaskName) -ForegroundColor red;
                    write-Host ("`t" + $_.Exception.Message) -ForegroundColor Red; 
                    break;
                }
            } 
        }
    }
}


Clear-Host;
Move-ScheduledTask; 

https://gallery.technet.microsoft.com/Dplacer-des-tches-planifies-1eabc70d/view/Discussions#content

Usage: Save the code snippet into a file named Move.ps1 or something then execute it. It will ask you which tasks you wish to move in a prompt and you can even filter it with by State/Name etc. then just click ok and wait for the magic to happen!

Share:
22,726

Related videos on Youtube

G33kKahuna
Author by

G33kKahuna

Updated on July 09, 2022

Comments

  • G33kKahuna
    G33kKahuna almost 2 years

    Is there a command to create a Schedule task folder in Windows 2008? I am trying to use SchTasks.exe to create the tasks and would like to put these tasks under a task folder. Essentially, inside task scheduler, add a new folder and add multiple tasks underneath the folder. From UI there is an option to create a folder but not sure about command reference

    Thanks in advance

    • G33kKahuna
      G33kKahuna about 12 years
      few trials and solved the problem; the key is using "\" in the name. Sample schtask.exe command line, schtasks /create /xml "MyTask.xml" /tn "My Task Folder\My New Task". this creates a folder "My Task Folder" and creates a new task "My New Task"
  • G33kKahuna
    G33kKahuna about 12 years
    Thanks, figured it out! refer to my answer
  • VCD
    VCD about 10 years
    For information, on Windows Server 2012 R2 this solution doesn't work and will return the following error. ERROR: The creation of the scheduled task failed. Reason: The Task Name may not contain the characters: < > : / \ |
  • Peter Krassoi
    Peter Krassoi over 6 years
    @VCD: I just tested it on WS 2012 R2 and it worked for me to create task under a folder. It seems that MS corrected this behaviour.
  • Frantumn
    Frantumn about 4 years
    I am on Server 2012 R2 and I am getting the error @VCD noted. I have double quotes around the path name.