February 27, 2013

Register-ScheduledTasksEx add-on for PowershellPack

To manage the Task Scheduler service on Windows 8/2012, you can use the new Scheduledtasks module. On Windows Vista/7/2008/2008 R2 you have to download the PowershellPack (also included in Windows 7 Reskit) and install the taskscheduler module.
But there is one thing missing in the taskscheduler module, when you want to create a new task with the Register-ScheduledTasks : you can’t specified in witch folder you want it. So I’ve created a new cmdlet (Register-ScheduledTasksEx) that allow you to specified to folder to use. To install this new cmdlet (kind of a add-on) with the taskscheduler module, just follow these 2 steps :

Step 1 : Create the file Register-ScheduledTasksEx.ps1

Create a new file named Register-ScheduledTasksEx.ps1 in the directory where the taskscheduler module is installed (generally %USERPROFILE%\Documents\WindowsPowerShell\Modules) and paste the following code in it.
function Register-ScheduledTaskEx 
        Registers a scheduled task.
        Registers a scheduled task.
        Modified to support Folders (path)
    # The name of the scheduled task to register

    # The path of the scheduled task to register
    # The Scheduled Task to Register
    # The name of the computer to connect to.
    # The credential used to connect
    begin {
        Set-StrictMode -Off
    process {
        if ($task.Definition) { $Task = $task.Definition } 
        foreach ($c in $computerName) {
            $scheduler = Connect-ToTaskScheduler -ComputerName $c -Credential $Credential            
            if ($scheduler -and $scheduler.Connected) {
                $folder = $scheduler.GetFolder($path)
                if ($Credential) {
                } else {

Step 2 : Modify TaskScheduler.psm1 file

Add the following line to the TaskScheduler.psm1 file (In the TaskScheduler module directory).
. $psScriptRoot\Register-ScheduledTaskEx.ps1


Here’s how to use the new cmdlet :
import-module TaskScheduler
$ScheduledTask = New-Task
$ScheduledTask | add-TaskAction -path "Notepad.exe" -Arguments "" -WorkingDirectory ""| out-null
# Note : The folder "\MyCustomFolder" must already  exist in the TaskScheduler
$ScheduledTask | Register-ScheduledTaskEx -Name "MyCustomScheduledTask" -path "\MyCustomFolder" -ComputerName Localhost | out-null


naviwhack said...

Just to point out your version still has the same typo the original does:


ParadisJ said...

Thanks naviwhack, I missed that one.
I've updated the script.

Post a Comment