Removing old snapshots in PowerCLI

Removing old snapshots in PowerCLI

Manually cleaning up Virtual Machine Snapshots can be a tedious process from the GUI.  Luckily for us, removing them with PowerCLI is very easy.

First, let’s get comfortable with at least looking at what snapshots exist on a VM:

Get-VM TestVM | Get-Snapshot

This gives us the following information (which isn’t exactly useful to us):

//todo add snapshot listing//

How about piping the output and selecting some more useful (to me) information:

Get-VM TestVM | Get-Snapshot | Select VM, Name, Created, SizeGB

//todo add formatted listing//

Let’s move onto removing all snapshots from a specific Virtual Machine:

Get-VM TestVM | Get-Snapshot | Remove-Snapshot

This will prompt you to confirm snapshot removal for each snapshot on the VM.

We can suppress the confirmation by toggling the confirm Boolean to false:

Get-VM TestVM | Get-Snapshot | Remove-Snapshot -confirm:$false

The above command will delete each snapshot one by one.  If we’re planning to delete all the snapshots, why not just select the last snapshot and delete all the children (note:  if you have multiple snapshot chains on the VM this won’t have the intended effect), similar to a Delete All:

Get-VM TestVM | Get-Snapshot | Select-Object -Last 1 | Remove-Snapshot -Confirm:$false -RemoveChildren

That covers the basics for removing a snapshot from a single VM, but often we’ll need to delete snapshots from a collection of VMs.

To remove all snapshots on all VMs from a specific VM Folder:

Get-Folder ExampleFolder | Get-VM | Get-Snapshot | Remove-Snapshot -confirm:$false -RemoveChildren

To remove all snapshots from all VMs older than Feb 1, 2017:

Get-VM | Get-Snapshot | Where-Object {$_.Created -lt (Get-Date 1/Feb/2017)} | Remove-Snapshot -Confirm:$false

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s