Author Posts

October 31, 2014 at 1:21 pm

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

October 31, 2014 at 3:19 pm

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

October 31, 2014 at 3:25 pm

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.

October 31, 2014 at 3:40 pm

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