PowerCLI

 View Only

 Scheduled task to remove old snapshots

net tech's profile image
net tech posted Feb 20, 2026 09:00 PM

Hi,

I have 1000+ vms environment.

Looking to simplify some of the daily tasks by adding a scheduled task to all VMs to delete a snapshot older than 1 day.

As I walk through the GUI there is a way to create a scheduled task that removes snapshots older than X number of days on a VM, however when I pull the properties of the task using PowerCLI I am not finding where X number of days is stored. 

Hopping I would be able to clone this task to the rest of the VMs or create a new task on all VMs with the parameters of the this task. Just cant figure out where the number of days for the snapshot age is stored. 

Thanks!

Name                           Port  User
----                           ----  ----
vcsa01.corp.local   443   CORP\AdmJohn
---------------------------------------------
Task Name:        VMNAME - Delete Snapshots
Description:
Enabled:          True
State:            success
Next Run Time:    02/23/2026 17:58:00
Last Run Time:
Last Result:
 
=== Scheduler Details ===
 
 
Hour       : 17
Minute     : 58
Interval   : 1
ActiveTime : 2/23/2026 12:58:00 PM
ExpireTime :
 
 
=== Action Details ===
 
Name     : RemoveAllSnapshots_Task
Argument : {VMware.Vim.MethodActionArgument, VMware.Vim.MethodActionArgument}

Thank you

# Connect-VIServer vcenter.domain.com

$vmName = "VMNAME"

# Get VM view
$vm = Get-VM -Name $vmName
$vmView = Get-View $vm.Id

# Get Scheduled Task Manager
$si = Get-View ServiceInstance
$scheduledTaskManager = Get-View $si.Content.ScheduledTaskManager

# Get all scheduled tasks
$allTasks = $scheduledTaskManager.ScheduledTask | ForEach-Object { Get-View $_ }

# Filter tasks attached to this VM
$vmTasks = $allTasks | Where-Object {
    $_.Info.Entity.Value -eq $vmView.MoRef.Value
}

# Show full details
foreach ($task in $vmTasks) {

    Write-Host "---------------------------------------------"
    Write-Host "Task Name:        $($task.Info.Name)"
    Write-Host "Description:      $($task.Info.Description)"
    Write-Host "Enabled:          $($task.Info.Enabled)"
    Write-Host "State:            $($task.Info.State)"
    Write-Host "Next Run Time:    $($task.Info.NextRunTime)"
    Write-Host "Last Run Time:    $($task.Info.LastRunTime)"
    Write-Host "Last Result:      $($task.Info.LastTaskResult)"

    Write-Host ""
    Write-Host "=== Scheduler Details ==="
    $task.Info.Scheduler | Format-List *

    Write-Host ""
    Write-Host "=== Action Details ==="
    $task.Info.Action | Format-List *

    Write-Host ""
}
JStars's profile image
JStars

In the vSphere Client “Delete snapshots older than X days” scheduled task, X is stored in the scheduled task action arguments as a VMware.Vim.SnapshotSelectionSpec object, specifically RetentionDays, and the action method is RemoveAllSnapshots_Task.

When you pull the task with Get-View, you’ll see something like:

Info.Action.Name = RemoveAllSnapshots_Task

Info.Action.Argument = two MethodActionArgument entries

The second argument’s .Value is a SnapshotSelectionSpec containing RetentionDays

also, I would run the script from a centralized location/server

JStars's profile image
JStars

here an example:

$RetentionDays = 1

$si  = Get-View ServiceInstance
$stm = Get-View $si.Content.ScheduledTaskManager

# Example: apply to all VMs in a folder 
$targetVMs = Get-Folder "MyFolder" | Get-VM

foreach ($vm in $targetVMs) {
  $vmView = Get-View $vm.Id

  $spec = New-Object VMware.Vim.ScheduledTaskSpec
  $spec.Name        = "$($vm.Name) - Delete Snapshots (>${RetentionDays}d)"
  $spec.Description = "Auto-delete snapshots older than $RetentionDays day(s)"
  $spec.Enabled     = $true

  # Scheduler example: daily at 02:00 
  $spec.Scheduler = New-Object VMware.Vim.DailyTaskScheduler
  $spec.Scheduler.ActiveTime = (Get-Date).Date.AddHours(2)
  $spec.Scheduler.Interval   = 1

  # Action: RemoveAllSnapshots_Task with SnapshotSelectionSpec.RetentionDays
  $spec.Action = New-Object VMware.Vim.MethodAction
  $spec.Action.Name = "RemoveAllSnapshots_Task"

  $arg1 = New-Object VMware.Vim.MethodActionArgument
  $arg2 = New-Object VMware.Vim.MethodActionArgument
  $arg2.Value = New-Object VMware.Vim.SnapshotSelectionSpec
  $arg2.Value.RetentionDays = $RetentionDays

  $spec.Action.Argument += $arg1
  $spec.Action.Argument += $arg2

  $stm.CreateScheduledTask($vmView.MoRef, $spec)
}