Quick PowerCLI to Get SnapShots and Size

Just a quick VMware PowerCLI one liner to display the number of snapshots for each VM and the total size in MB of the snapshots for each VM.

Get-VM | Format-Table Name, @{Label="NumSnapshots";Expression={(Get-Snapshot -VM $_ | Measure-Object).Count}}, @{Label="TotalSnapShotSizeMB";Expression={(Get-Snapshot -VM $_ | Measure-Object -Sum SizeMB).Sum}}

The cmdlet Get-VM gets all the VMs in the environment. The Get-Snapshot cmdlet counts the number of snapshots and then provides a sum of the total size in MB. Change SizeMB to SizeGB to get the sum of snapshot size in GB.

snapshot-powercli

4 thoughts on “Quick PowerCLI to Get SnapShots and Size

  • February 21, 2017 at 2:45 pm
    Permalink

    Hi vHersey

    great post!
    I don’t like all these decimal and I would like to round the value using [math]::round function but I couldn’t make it work

    Do you have any suggestion?

    Reply
  • October 23, 2017 at 11:53 am
    Permalink

    Appreciate it but – not a fan of the above command myself, it doesn’t even autosize the table in the output, why not:

    Get-Snapshot * | Select-Object -Property Name, SizeGB, VM, PowerState, Children | Sort-Object -Property sizeGB -Descending | ft -AutoSize

    Reply
  • June 15, 2018 at 7:58 am
    Permalink

    @marco

    you can round the sum like this, I’ve changed here also to use GB instead of MB

    {[math]::round((Get-Snapshot -VM $_ | Measure-Object -Sum SizeGB).Sum)}}

    Reply
  • December 12, 2018 at 8:36 am
    Permalink

    Thanks for the guide, it helped me when I started with PowerCLI, and I ended up using the following command:

    # List all snapshots:
    Get-VM | Get-Snapshot | Select VM,Name,Description,@{Label=”Size”;Expression={“{0:N2} GB” -f ($_.SizeGB)}},Created

    Then, I can delete all the snapshot (this is a violent command):

    #Delete all snapshots:
    Get-Snapshot -VM * | Remove-Snapshot -RunAsync -Confirm:$False

    I am also using similar scripts to target a group of server (in an array) and create / delete snapshots easily. In my day 2 day job, it allows me to, let’s say snapshot all Linux servers at once, then I’ll install updates (via Ansible), then a few days later delete only the “before_update” snapshots.

    Automation is fun, and powerful!

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

nineteen − fifteen =