Using PowerShell to get a list Virtual Machine Snapshots in VMware ESXi 4.1

Hi All,

VMware snapshots can be a very good insurance policy when making changes to a VM. Its also important to make sure not to leave old snapshots laying around as this will cause you problems later. Depending on the amount of VMs you have, it may be difficult and time consuming to manually go thru all your VMs to check for snapshots. In this blog I will explain a few techniques I have used to report on VM snapshots. We will be using the “Get-VM” cmdlet and piping it to the “Get-Snapshot” to get the information we need in the examples below. To do anything with PowerShell in VMware you will need to install VMware vSphere PowerCLI. You will then need to open PowerCLI connect to your VMWare vCenter server by typing “Connect-VIServer vCenterServerName”.

To get a list of all VM Snapshots for VMs managed by vCenter you can type the following command:

get-vm | get-snapshot | format-list

The command above will give you the following properties:

Description
Created
Quiesced
PowerState
VM
VMId
Parent
ParentSnapshotId
ParentSnapshot
Children
SizeMB
IsCurrent
IsReplaySupported
ExtensionData
Id
Name
Uid

After seeing a list of properties for all VMs with snapshots, you may decide you only want to see certain properties. For example, you want to list all VMs with snapshot but you only want list the VM and the name of the snapshot you could simply add these properties after “Format-List” as shown below:

get-vm | get-snapshot | format-list vm,name

If you would like to write that information to a text file for later review or to send it to other Admins so they may clean up their mess, type the same command as above but use the “Out-File” cmdlet to pipe it to text file. In this example we pipe the output to a file on the C drive named snaps.txt:

get-vm | get-snapshot | format-list | out-file c:\snaps.txt

Now lets say you want a list of snapshots for only a subset of virtual machines in vCenter and you have your VMs organized in different folders under “VMs and Templates”. In this case you can run the command so it only targets a particular folder. For example if you had a folder in vCenter that contains 5 lab virtual machines and the name of the folder was called “My Lab”, you would type the following command to list all snapshots for VMs in the “My Lab” folder:

get-vm -location “My lab” | get-snapshot | format-list

I hope that this will give you a quick and easy way to get a good inventory of you Virtual Machine Snapshots as well help in keeping your environment clean. If you have any comments please feel free to post!

Related Links:

Using PowerShell to Gracefully Shut Down Multiple Virtual Machines in VMware

Using PowerShell to Remove Virtual Machine Snapshots in VMware ESXi 4.1

Using PowerShell to create a Virtual Machine Inventory in VMware and Export it to a CSV File

Using PowerShell to Create Virtual Machine Snapshots in VMware

Using PowerShell to View  Virtual Machine Disks (VMDK) Information in VMware and Export to a CSV File

Using PowerShell to View Datastore and Virtual Machine Space Usage Information in VMware

 

Share Button
Author: BC

BC is a Pipe2text.com contributor and has been an IT Professional for 15 years. He currently works as a Sr. Systems Engineer at a private firm. He enjoys working with Active Directory, Exchange and VMware among other technologies.


17 Responses to Using PowerShell to get a list Virtual Machine Snapshots in VMware ESXi 4.1

  1. Wei-Yen Tan says:

    excellent article. By reading this it has further cemented my understanding of how powershell/cli can pipe info into something else.

    Also I used remove-snapshot cmdlet to automatically remove all the snap shots.

    I had 16 VMs that could potentially have snapshots and going through was tedious but this one liner got it going pretty much straight away! All i had to do was to wait for it to clean itself up.

    • BC says:

      I’m glad you found this article helpful. I plan to write something in the future about removing the snapshots as well. Thanks for the feedback!

  2. Ali says:

    Hi BC:
    thanks for the article it was very useful.

    i was just curious if you have already wrote the script to remove snapshots of particular VMs in the vcenter, please share it with us.

  3. BC says:

    Hi Ali,

    Please see the newly posted blog “Using PowerShell to Remove Virtual Machine Snapshots” in VMware ESXi 4.1 located at http://pipe2text.com/?page_id=1953. Thanks for the feedback.

  4. richard says:

    very helpfull, thank you! 🙂

  5. Alex says:

    Thanks for the useful info.
    A question , Is there any way to “Go to” a specific snapshot by using PowerCLI?(I mean change the current)
    It’s easy to do this in vSPhere client using “Snapshot Manager” , but so far I didnt find anything to do it with PowerCLI.

    Thanks.

    • BC says:

      Are you asking if there is a PowerShell command to revert back to a snapshot? I haven’t looked into it but if there is I hope to post it at some point.

  6. Kusa says:

    Nice article, appreciate it !!

  7. ash says:

    Are you able to add a sort command to sort by date or by size etc..?

    • BC says:

      You can add | sort and the property.
      Sorting by size would be as follows:
      get-vm | get-snapshot | sort SizeMB | format-list vm,name,SizeMB,created

      Sorting by date created would be as follows:
      get-vm | get-snapshot | sort created | format-list vm,name,SizeMB,created

      You can also use -descending parameter to change the sort to descending as shown below:

      get-vm | get-snapshot | sort created -descending | format-list vm,name,SizeMB,created

      Hope this helps!

  8. Sanjiv says:

    Now that I know how to write a script to get all the snapshots of my vCenter server, I would like to know how I can save these scripts so I can run in one click. I tried saving them as ps1 (PowerShell script) but it didnt work. How can save and run the script?

  9. BC says:

    Hey Sanjiv, Try the commands below and let me know if it works for you. Replace VCENTERSERVERNAME with the name of you vCenter server. Make sure to run this with an account that has access. Save the following to a .ps1 file:

    Add-PSSnapin VMware.VimAutomation.Core
    Connect-VIServer VCENTERSERVERNAME
    get-vm | Get-Snapshot

  10. Sanjiv says:

    Adding Add-PSSnapin VMware.VimAutomation.Core and saving it in .ps1 didn’t work. It opens up PowerShell and closes itself automatically without doing anything. I have a right access and I am using “AllSigned” execution policy. Any idea why it is not working?

  11. Sanjiv says:

    It worked once I set the execution policy to RemoteSigned. Thank you so much.

  12. Abhilash Shashidhara says:

    Simple and good. Thanks a ton. This is called hassle free writing.

  13. Phil Mather says:

    THanks for the article..very useful for a newbie to PowerCLI
    Phil

Leave a Reply

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