Manage snapshots using powershell

Reading Time: 3 mins

Snapshots:

 

Connect to VC using any of the below commands

Connect-VIServer <vc name> 

or

Connect-VIServer <vc name> -user <username> -password <password>


How to take snapshot using powershell?

Here is the example I am using to take memory snapshot for a specific vm:

get-vm <vm name> | New-Snapshot -Name "snapshot Test" -Description "Taken today" -Memory

If you have a requirement to take snapshots for all vm's in a cluster or host then use commands as :

get-cluster <cluster name> | get-vm | New-Snapshot -Name "snapshot Test" -Description "Taken today" -Memory

get-vmhost <host name> | get-vm  | New-Snapshot -Name "snapshot Test" -Description "Taken today" -Memory

  • If memory snapshot is not required, then remove -Memory from commands
  • If the VM is running and it has virtual tools installed, you can opt for a quiescent snapshot with –Quiesce parameter.  This has the effect of saving the virtual disk in a consistent state.
  • If you don’t want to be confirmation prompts, include –confirm:$False

Below powershell command lists the snapshots of virtual machines in the connected VC that are aged more than 15 days, if you are looking to get the snapshot list older than "n" number of days then replace  15 (highlighted in bold) with "n" days.

Get-VM | Get-Snapshot | ?{$_.Created -lt (Get-Date).AddDays(-15)} | Select-Object vm, @{N="Size";E={"{0:N2} GB" -f ($_.SizeGB)}} | Sort-Object vm | Format-Table

To list vm snapshots any specific cluster: get-cluster <cluster name> | get-VM | get-Snapshot | ?{$_.Created -lt (Get-Date).AddDays(-15)} | Select-Object vm, @{N="Size";E={"{0:N2} GB" -f ($_.SizeGB)}} | Sort-Object vm | Format-Table

To list vm snapshots any specific host: get-vmhost <host name> | get-VM | get-Snapshot | ?{$_.Created -lt (Get-Date).AddDays(-15)} | Select-Object vm, @{N="Size";E={"{0:N2} GB" -f ($_.SizeGB)}} | Sort-Object vm | Format-Table

Delete the snapshots of all vm's in connected vCenter that are aged older than 15 days:

Get-VM | Get-Snapshot | ?{$_.Created -lt (Get-Date).AddDays(-15)} | remove-snapshot

  • Some snapshots may have child snapshots if you are taking many during a maintenance, so you can also use –RemoveChildren to clean those up as well.
  • Removing a snapshot can be a long process so you might want to take advantage of the –RunAsync parameter.

Below powershell command lists the snapshots of virtual machines in the connected VC that have size more than 10 GB, if you are looking to get the snapshots of any other size, then replace 10 (highlighted in bold) with "n".

get-vm | Get-Snapshot | Where-Object{$_.SizeGB -gt "10"} | Select-Object vm, @{N="Size";E={"{0:N2} GB" -f ($_.SizeGB)}} | Sort-Object vm | Format-Table

Delete the snapshots that have size greater than 10 GB:

get-vm | Get-Snapshot | Where-Object{$_.SizeGB -gt "10"} | remove-snapshot

 

Above commands might require some modifications based on the requirements.. Please feel free to ask me in case of any help.