
While working on a project for a client where many of the Windows Systems are 2008R2 with PowerShell 3.0 or lower, there was a need to automate disabling scheduled tasks to ensure consistency across the XenApp/XenDesktop estate.
This is something that can be done easily in PowerShell 4.0+ using Get-scheduledTask cmdlet. But in PowerShall 3.0 and earlier, it gets a bit trickier.
Based on the script by Jaap Brasser where he enumerates all tasks subfolders, I have written a new code which can accept an array of schedule tasks names, along with a switch to enable/disable them.
How to use
- Add the below two functions in your PowerShell session on the system where the tasks should be disabled.
- Call the Set-TaskStatus function and pass the status (Enabled, Disabled) and the tasks names (Array)
Example
1 2 3 |
Set-TaskStatus -Tasklist "WrongName","Office Automatic Updates" -Status disabled |
The output will be something like this

The Script
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
function Get-AllTaskSubFolders { [cmdletbinding()] param ( # Set to use $Schedule as default parameter so it automatically list all files # For current schedule object if it exists. $FolderRef = $Schedule.getfolder("\") ) if ($FolderRef.Path -eq '\') { $FolderRef } if (-not $RootFolder) { $ArrFolders = @() if(($folders = $folderRef.getfolders(1))) { $folders | ForEach-Object { $ArrFolders += $_ if($_.getfolders(1)) { Get-AllTaskSubFolders -FolderRef $_ }#end of IF }#end of ForEach }#end of IF $ArrFolders }#end of IF } function Set-TaskStatus { [cmdletbinding()] Param ( [ValidateSet("Enabled","Disabled")][string]$Status, [parameter(ValueFromPipeline = $True,ValueFromPipeLineByPropertyName = $True)] [string]$Computername = 'localhost', [parameter(Mandatory)] [string[]]$Tasklist ) try { $schedule = New-Object –ComObject ("Schedule.Service") } catch { Write-Warning "Schedule.Service COM Object not found, this script requires this object" return } $Schedule.connect($computername) $AllFolders =Get-AllTaskSubFolders if ($Status -eq "Enabled") {$TaskStatus = $True} if ($Status -eq "Disabled") {$TaskStatus = $False} $Hits = @() foreach ($tTask in $TaskList) { $AllFolders | % { $curFolder = $schedule.GetFolder($_.path) $curFolder.gettasks(1) | % { if ($_.name -eq $tTask) { $_.enabled = $TaskStatus $Hits += $_.name Write-host "The task: $tTask is now set to $TaskStatus" } #end of IF } #end inner loop } #End Folders loop If ($Hits -notcontains $tTask) { Write-host "The task: $tTask does not exist" } } } |
Disable/Enable Scheduled tasks using Powershell and COMObject