How can I disable a scheduled task using Powershell?

27,601

Solution 1

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
}

Solution 2

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

Solution 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}
Share:
27,601

Related videos on Youtube

CBlumey
Author by

CBlumey

Updated on September 18, 2022

Comments

  • CBlumey
    CBlumey over 1 year

    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?

  • Tim Ferrill
    Tim Ferrill almost 10 years
    That's great for Windows Server 2012, but he stated he's using Windows Server 2008 R2.
  • Mathias R. Jessen
    Mathias R. Jessen almost 10 years
    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).
  • Tim Ferrill
    Tim Ferrill almost 10 years
    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.
  • Signal15
    Signal15 about 9 years
    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