8

I have a web application that runs on Windows Server 2008 R2, which has a large number of scheduled tasks which take care of all the backend stuff. When I do a software deployment which touches the database, I need to disable all the scheduled tasks. Currently I have a long checklist that I need to step through manually, disabling each scheduled task as I go - surely this is a job ripe for automation with Powershell.

Unfortunately, the Powershell documentation seems to be fairly coy about how you disable an existing scheduled task (and of course, re-enable it once the release has been completed successfully). I can get a list of ready, running or disabled tasks, but what next?

7 Answers 7

14

You can use the COM-based Task Scheduler Scripting objects:

($TaskScheduler = New-Object -ComObject Schedule.Service).Connect("localhost")
$MyTask = $TaskScheduler.GetFolder('\').GetTask("My Task")
$MyTask.Enabled = $false

To enable the task again:

$MyTask.Enabled = $true

The above will only work if the shell is elevated and you are a local Administrator on the server. You could make your own cmdlet with the above:

function Disable-ScheduledTask
{
    param([string]$TaskName,
          [string]$ComputerName = "localhost"
         )

    $TaskScheduler = New-Object -ComObject Schedule.Service
    $TaskScheduler.Connect($ComputerName)
    $TaskRootFolder = $TaskScheduler.GetFolder('\')
    $Task = $TaskRootFolder.GetTask($TaskName)
    if(-not $?)
    {
        Write-Error "Task $TaskName not found on $ComputerName"
        return
    }
    $Task.Enabled = $False
}
1
  • 2
    Note: this doesn't appear to work for tasks created with at.exe. (Example: AT 09:00 /every:SUNDAY shutdown.exe /r /f /d p:4:1 /c "Reboot via SchTask (AT Job)"). The error you'll get is 0x80041327; The task has properties that are not compatible wiht previous versions of windows
    – Signal15
    Feb 26, 2015 at 19:25
5

If you're just trying to stop ALL of the tasks, it may be easier to just stop the Task Scheduler service. The ScheduledTasks Module isn't available until Windows Server 2012, so managing tasks isn't as straightforward as stopping and starting a service:

Stop-Service Schedule
Start-Service Schedule

If that doesn't work for you schtasks.exe can still be used from PowerShell to manage individual tasks:

schtasks.exe /CHANGE /TN "My Task" /DISABLE
schtasks.exe /CHANGE /TN "My Task" /ENABLE
2
  • 1
    From 2008/Vista and forward, Windows itself use the Task Scheduler heavily for a wide variety of system tasks. Depending on the duration, stopping the Task Scheduler service might not be such a great idea (although it is easy). Jun 5, 2014 at 13:33
  • Sure. The use case given was for disabling the tasks during maintenance, so that could be a viable option. Also gave the second option in case that wouldn't meet the need. Jun 5, 2014 at 13:44
3

Is this what you are looking for (Disable-ScheduledTask)?

1
  • 4
    That's great for Windows Server 2012, but he stated he's using Windows Server 2008 R2. Jun 5, 2014 at 13:02
3

Even a better way where you don't need to specify names of the tasks:

($TaskScheduler = New-Object -ComObject Schedule.Service).Connect("localhost")
$TaskScheduler.GetFolder('\').GetTasks(0) | % {$_.Enabled = $false}
0

This is perfect! It works better than Get-ScheduledTask since I am in a 2008/2012 mixed environment. I added a few lines to query AD and invoke this command against a group of computers. My goal was to disable the task of "Server Manager" opening every time a user logs in. Presto! all gone!

$C=Get-ADComputer -SearchBase 'OU=Computers,dc=domain,dc=com' -Filter * | foreach {$_.name}

Invoke-Command $C {
($TaskScheduler = New-Object -ComObject Schedule.Service).Connect("$C")
$TaskScheduler.GetFolder('\Microsoft\Windows\Folder').GetTasks(0) | % {$_.Enabled = $false}
}
0

Look at this function

Function Control-Tasks
{
    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory=$true)][String]$FolderPath,
        [Parameter(Mandatory=$true, ParameterSetname='EnableTask')][Switch]$Enable,
        [Parameter(Mandatory=$true, ParameterSetname='DisableTask')][Switch]$Disable,
        [Parameter()]$TaskScheduler,
        [Parameter()][Switch]$Recursive
    )

    If ($TaskScheduler -eq $null) {
        $TaskScheduler = New-Object -ComObject Schedule.Service
        $TaskScheduler.Connect("localhost")
    }

    $curFolder = $TaskScheduler.GetFolder($FolderPath)

    $curFolder.GetTasks(1) | ForEach-Object {
        If ($Enable) {
            $_.Enabled = $True
            Write-Host "Enable $($_.Path)"
        }

        If ($Disable) {
            $_.Enabled = $False
            Write-Host "Disable $($_.Path)"
        }
    }

    If ($Recursive) {
        $curFolder.GetFolders(1)  | ForEach-Object {
            If ($Enable) {
                Control-Tasks -FolderPath $_.Path -Enable -TaskScheduler $TaskScheduler -Recursive
            }
            If ($Disable) {
                Control-Tasks -FolderPath $_.Path -Disable -TaskScheduler $TaskScheduler -Recursive
            }
        }
    }

}

For example, you can disable all tasks under folder "\mysql" by calling Control-Tasks -FolderPath "\mysql" -Disable -Recursive

This code snippets come from How to disable/enable scheduled task in batch by PowerShell, you can download the complete sample archive there.

0

this worked for me

    Get-ScheduledTask -TaskName "TestJob"  | Stop-ScheduledTask

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .