Deleting All Scheduled Tasks using the Schedule.Service Com Object

This topic contains 3 replies, has 2 voices, and was last updated by Profile photo of I Am Sir Ask Alot I Am Sir Ask Alot 2 years, 1 month ago.

  • Author
    Posts
  • #20217
    Profile photo of I Am Sir Ask Alot
    I Am Sir Ask Alot
    Participant

    I wrote up a pretty cool advanced function that will delete individual scheduled tasks, but what if someone wants to just delete all scheduled tasks within a parent folder rather than have to name each one? For instance, here is a script I found online that deletes individual tasks:

    # create Task Scheduler COM object
    $TS = New-Object -ComObject Schedule.Service
    # connect to local task sceduler
    $TS.Connect($env:COMPUTERNAME)
    # get tasks folder (in this case, the root of Task Scheduler Library)
    $TaskFolder = $TS.GetFolder("\")
    # get tasks in folder
    $Tasks = $TaskFolder.GetTasks(1)
    # define name of task to delete
    $TaskToDelete = "MyTask"
    # step through all tasks in the folder
    foreach($Task in $Tasks){
        if($Task.Name -eq $TaskToDelete){
            Write-Host ("Task "+$Task.Name+" will be removed")
            $TaskFolder.DeleteTask($Task.Name,0)
        }
    }

    So how would we tell the DeletTask method to just delete all of the tasks within a task folder rather than having to define each one? Is there a wild card (i.e. the asterisk *) I can use that I am not aware of?

    Thanks

  • #20218
    Profile photo of Tim Pringle
    Tim Pringle
    Participant

    Try :-

    for exact

    Get-ScheduledTask | Where TaskPath -eq 'PutYourFolderPathHere' | Unregister-ScheduledTask -Confirm:$false

    for wildcards

    Get-ScheduledTask | Where TaskPath -like 'ParentFolderPath*' | Unregister-ScheduledTask -Confirm:$false
  • #20219
    Profile photo of I Am Sir Ask Alot
    I Am Sir Ask Alot
    Participant

    I am using the Schedule Service com object, not the Scheduled Tasks cmdlets. I am running this on Win 7 systems and the Scheduled Tasks cmdlets are available only on Win 8.1 and Server 2012 R2, from what I understand.

  • #20220
    Profile photo of I Am Sir Ask Alot
    I Am Sir Ask Alot
    Participant

    Disregard, I figured out how to do it. Thanks anyway man

You must be logged in to reply to this topic.