How can I disable a scheduled task using Powershell?

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:

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