I've found this nifty little script that will let me delete snapshots older than (in this case) 30 days:
connect-viserver -server
Get-VM | Get-Snapshot | Where { $_.Created -lt (Get-Date).AddDays(-30)} | select Name, Created
Is it possible to have a script delete all but the two most recent snapshots? For instance, I have a snapshot tree like this:
1
2
3
4
And the naming for all VMs might not be the same for those 4. I want to get rid of the first 2 snapshots so that only 3 and 4 remain. I also can't base it on the number of days that have gone by as the first snapshot might not have been created on the same day for different VMs. Is there some kind of attribute that can be used perhaps? So far, I've only found the above script or one that will delete all snapshots. Thank you for your help!
Hi,
welcome to the VMware Communities.
The next script will delete all but the two most recent snapshots for all virtual machines:
connect-viserver -server vCenter -user Username -password Password Get-VM | Foreach-Object { $Snapshots = $_ | Get-Snapshot $SnapshotCount = ($Snapshots | Measure-Object).Count If ($SnapshotCount -gt 2) { $RemoveCount = $SnapshotCount - 2 $Snapshots | ` Sort-Object -Property Created | ` Select-Object -First $RemoveCount | ` Remove-Snapshot -Confirm:$false } }
Regards, Robert
Hi,
welcome to the VMware Communities.
The next script will delete all but the two most recent snapshots for all virtual machines:
connect-viserver -server vCenter -user Username -password Password Get-VM | Foreach-Object { $Snapshots = $_ | Get-Snapshot $SnapshotCount = ($Snapshots | Measure-Object).Count If ($SnapshotCount -gt 2) { $RemoveCount = $SnapshotCount - 2 $Snapshots | ` Sort-Object -Property Created | ` Select-Object -First $RemoveCount | ` Remove-Snapshot -Confirm:$false } }
Regards, Robert
Thank you, Robert! That's amazing, just what I was looking for.