PowerCLI

 View Only

 Having issues figuring out how to code the deletion of vCenter scheduled tasks

Jump to  Best Answer
jonebgood_157's profile image
jonebgood_157 posted May 28, 2025 12:22 PM

Need some help or direction in trying to figure out how to delete a lot of vCenter Scheduled Tasks that were once setup and "ran once". They are set to never run again, so I want to cleanup. Unfortunately, the silly vCenter UI doesn't allow you to click, shift/select in bulk.  Was going to write a script but not finding the correct commands.  The scheduled tasks have various names and I want to basically "find all vcenter scheduled tasks older than 60 days and delete/purge". Thank you in advance.

bwuch's profile image
Broadcom Employee bwuch  Best Answer

I had wrote some functions to deal with vCenter scheduled task removal a long time ago (March 2013 -- https://enterpriseadmins.org/blog/scripting/get-vcenter-scheduled-tasks-with-powercli-part-1/).  There were 3 different posts, each with a function or two that could be used to create/remove some scheduled tasks.  The code could use some improvement, but I just tested with vCenter Server 8.0 and they still got the job done.  I've copied all the functions into a single powershell file for ease of use (https://github.com/bwuch/PowerShell/blob/master/VIScheduledTasks.ps1).  If you dot source that file (or simply copy/paste those functions into a powershell window), the following one liner should find all vCenter scheduled tasks that do not have a next run time defined and then invoke the RemoveScheduledTask() method to remove the task.

Get-VIScheduledTasks | Where-Object {$_.NextRunTime -eq $null} | ForEach-Object { Remove-VIScheduledTask -taskName $_.Name }

Alexandru Capras's profile image
Alexandru Capras

Hi there,

I've managed to write a small script with the help of @Zsoldier function from a previous post.
Make sure to copy and define the "Get-vCenterScheduledTask" function before running the script below, as the command will only work after the function is defined.

$limit = (Get-Date).AddDays(-60)
 
Get-vCenterScheduledTask | Where-Object {
 
$_.PrevRunTime -lt $limit
 
} | ForEach-Object {
 
$taskview = Get-View $_.ScheduledTask
 
$taskview.RemoveScheduledTask()
 
Write-Host "Deleted task: $($_.Name)"
 
}

In my example I used a limit for scheduled tasks older than 60 days (last run time).
jonebgood_157's profile image
jonebgood_157

@bwuch  This looks great; I'll test this out.  So if I wanted to add a condition for anything with next run time equal to null AND older than 60 days, do I just add that to the RemoveScheduledTask() method command line?

bwuch's profile image
Broadcom Employee bwuch

You could handle all of that in the Where-Object filter, like so:

Get-VIScheduledTasks | Where-Object {$_.NextRunTime -eq $null -AND $_.PrevRunTime -lt (Get-Date).AddDays(-60)} | ForEach-Object { Remove-VIScheduledTask -taskName $_.Name }

You can run Get-VIScheduledTasks to see other criteria that could be used for filtering, like entityname, state, the user who last modified the task, etc.