Tom Zarzycki
Tom Zarzycki

Reputation: 1

Script to search for specific task in scheduler and delete this task

I am quite new in this area and I am bit lost but when I have looked to: https://serverfault.com/questions/604673/how-to-print-out-information-about-task-scheduler-in-powershell-script ,this was partially answering to my question. I am looking for script which will allow me to search for existing task and delete him. Problem is in version of Powershell. In my revision Get-ShceduledTask does not exists and I have to look for solution how to search like with this cmdlet. I have to note also, I cant change or upgrade Powershell to better version.

So, summarizing, the below code works at some point, but if some could help me to figure out how to accomplish this?

$sched = New-Object -Com "Schedule.Service"
$sched.Connect()
$out = @()
$sched.GetFolder("\").GetTasks(0) | % {
    $xml = [xml]$_.xml
    $out += New-Object psobject -Property @{
        "Name" = $_.Name
        "Status" = switch($_.State) {0 {"Unknown"} 1 {"Disabled"} 2 {"Queued"} 3 {"Ready"} 4 {"Running"}}
        "NextRunTime" = $_.NextRunTime
        "LastRunTime" = $_.LastRunTime
        "LastRunResult" = $_.LastTaskResult
        "Author" = $xml.Task.Principals.Principal.UserId
        "Created" = $xml.Task.RegistrationInfo.Date
    }
}

$out | fl Name,Status,NextRuNTime,LastRunTime,LastRunResult,Author,Created

Upvotes: 0

Views: 1358

Answers (1)

postanote
postanote

Reputation: 16086

Continuing from my comment.

They are just files on your file system.

Task Scheduler 1.0 API uses...

Get-ChildItem -Path 'C:\Windows\Tasks'

...folder to create and enumerate tasks.

Task Scheduler 2.0 API uses...

Get-ChildItem -Path 'C:\Windows\System32\Tasks' 

...to create and enumerate tasks.

As well as in the registry:

Get-ChildItem -Path 'HKLM:\Software\Microsoft\Windows NT\CurrentVersion\Schedule\Taskcache\Tasks'
Get-ChildItem -Path 'HKLM:\Software\Microsoft\Windows NT\CurrentVersion\Schedule\Taskcache\Tree'

Of course, using Powershell (either the version on your system or ones you can proxy to your system via PSRemoting from another system) you just pass the task name to the cmdlet. Filter as needed of course and remove the -WhatIf when you are sure you have the ones you want.

Get-ScheduledTask | 
ForEach-Object {Unregister-ScheduledTask -TaskName $PSItem.TaskName -WhatIf}

Here is a function I have in my personal ModuleLibrary for this as well.

Function Get-ScheduledTasks
{
    [CmdletBinding(SupportsShouldProcess)]
    [Alias('gst')]

    Param
    (
    
    )

    schtasks.exe /query /v /fo csv | 
    ConvertFrom-Csv | 
    Where-Object { $PSItem.TaskName -ne 'TaskName' } | 
    Sort-Object TaskName | 
    Out-GridView -Title 'All scheduled tasks' -PassThru
}

Selecting from the Out-GridView will make that selection(s) available for other actions.

You could just as easily create a new function to delete them or add a switch parameter to the one above; adding this command:

schtasks.exe /delete /tn "MyTasks/Task1" /f

Upvotes: 1

Related Questions