Changing Scheduled Tasks with PowerShell

by Jun 21, 2013

PowerShell can read and also change any part of a scheduled task. Just make sure you have appropriate permissions and run PowerShell elevated.

Let's assume you wanted to automatically enable or disable a task trigger for a given task. Here's a sample that would do this:

It picks the scheduled task "RACTask" in the scheduled task container "Microsoft\Windows\RAC". It then looks at the task definition and selects the task trigger "RACTimeTrigger". It finally makes sure this trigger is enabled, then writes back the updated task definition, effectively enabling this trigger:

# connect to Task Scheduler:
$service = New-Object -ComObject Schedule.Service
$service.Connect($env:COMPUTERNAME)

# pick a specific task in a container:
$folder = $service.GetFolder('\Microsoft\Windows\RAC')
$task = $folder.GetTask('RACTask')

# get task definition and change it:
# (for example, enable a task trigger)
# (here, the task trigger "RACTimeTrigger" gets enabled)
$definition = $task.Definition
$definition.triggers | 
  Where-Object { $_.ID -eq 'RACTimeTrigger' } | 
  ForEach-Object { $_.Enabled = $true }

# write back changed task definition:
# 4 = Update
$folder.RegisterTaskDefinition($task.Name, $definition, 4, $null, $null, $null)

RegisterTaskDefinition() returns the task definition it just updated, so you can double-check whether your changes were correct. You can also examine the task definition to find additional settings you may want to automatically change and add to your code.

Twitter This Tip! ReTweet this Tip!