Get Snapshots older than X Days – PowerCLI

Finding the virtual machines running on snapshots is a very common task and in this post i am going to show you a one liner that can pull snapshots older than X days. Here we go!

This is very critical because of the bad effects snaoshots can have on the virtual machines. VM’s running on snapshots for more than 72hrs are not supported by VMware. Snapshot is a great feature but it is more harmful when VM’s continue to run for a longer period. I remember a situation back in 2013 where a VM  (on ESXi 4) was having an active snapshot and it was not deleted for a year for some reason and eventually that has eaten up all the datastore space causing every other VM on the datastore to crash. Let’s avoid getting into such situations.

Here is a one liner that gives you the list of VM’s having snapshots for more than 3 days.

Get-VM | Get-Snapshot | Where {$_.Created -lt (Get-Date).AddDays(-3)} | Select-Object VM, Name, Created

1

Observe that a few VM’s have multiple snapshots. So to remove them all at once, use the below command. Depending on the size of the snapshots, time for consolidation varies.

Get-VM | Get-Snapshot | Where {$_.Created -lt (Get-Date).AddDays(-3)} | Remove-Snapshot -Confirm:$false

Hope this was informative. Thanks!

Advertisements