A. Mikkelsen VMware ESX scripts, commands, tools and other nice to know things that will make your virtualization days easier!!!!

Tuesday, October 21, 2008

PowerShell script – List snapshots

Filed under: PowerShell,Scripting,Tools — Tags: , , , , — A. Mikkelsen @ 11:54 am

I have created at PowerShell script that obtains a list of VM’s that have snapshots attached. The output is then converted to a html file using the cmdlet ConvertTo-HTML.
And just to make it easier to obtain the html file, the script mails the html file to me as an attachment.

Ths script has been updated thanks to Ryan, to be able to handle multiable snapshots on each VM.

Get the script from the Downlaod section.


  1. Hallo.
    Good Job. Script is OK, but not work fine for 2 or more snapshots on one VM. Result is
    if ($snapshots.Length -ge 1){

    Bye SPACIR.

    Comment by Spacir — Wednesday, December 3, 2008 @ 13:03 pm

  2. To list VM’s with 1 or more snapshots I made the following change:

    if ($snapshots.Name.Length -ige 1 -or $snapshots.length){

    Comment by Ryan — Saturday, January 24, 2009 @ 1:58 am

  3. Thanks for the update Ryan.
    I have tested your code and updated the script.
    Thanks again.

    Comment by A. Mikkelsen — Wednesday, January 28, 2009 @ 9:41 am

  4. I really liked your script, but found that the following changes to your script were helpful for our group, so I thought that I would pass them along.

    Adding a date to the subject line:
    $strSubject = “Snapshot list – ” + (get-date -DisplayHint date)

    Since we are using Outlook I removed the $strBody and replaced the message body with the html you generated. That way I did not have to open the attachment to “see” the results:
    $msg.IsBodyHtml = 1
    $msg.Body = Get-Content $strOutFile

    If you would prefer to skip creating the output file you can remove the appropriate lines and change the above $msg.body to:
    $msg.Body = ($myCol | Sort-Object VM | ConvertTo-HTML -Head $head -Body $strMail)

    I also found a interesting article from Marc Lognoul on his Straight from the Cask! blog that adds the bell icon for those of us using Outlook:
    $msg.Headers.Add(“message-id”, “<3BD50098E401463AA228377848493927-1>”)

    Finally one comment, since Ryan provided you the code for dealing with the multiple snapshots, shouldn’t he also be added into the script header and be given the appropriate credit?

    Comment by John — Thursday, March 12, 2009 @ 16:12 pm

  5. Hi John,
    I have updated your post and the script.
    Many thanks for the updates – I love them 🙂
    This will make the script even better.

    Comment by A. Mikkelsen — Tuesday, March 17, 2009 @ 8:02 am

  6. Hello there, your script is great I use it all the time. Is there a way to have this script run indefinite.

    Comment by Clark Dickson — Thursday, November 4, 2010 @ 17:38 pm

  7. Sure, just schedule the script to run.
    It’s the most simple way.

    A. Mikkelsen

    Comment by A. Mikkelsen — Friday, November 5, 2010 @ 8:52 am

  8. Hi,
    I edit your script and wish get snapshot list from txt file.
    # Get the list of VM’s
    $vms = Get-Content “c:\list.txt”
    Its run and send mail but on the mail i cant see any vm computer name …
    Snapshot name, created and description cells are ok .
    where make i mistake ?

    Comment by Cem — Friday, February 1, 2013 @ 11:01 am

  9. Hi Cem,
    The problem is that “$vms = Get-VM” from the original script is a collection of information for each VM. And the “$vms = Get-Content “c:\list.txt”” you are using, is only a list of vm names.

    If you want it to work, replace all from
    “# Get the list of VM’s
    $vms = Get-VM” with the below.

    $vmlist = Get-Content “c:\list.txt”

    $myCol = @()
    ForEach ($vm in $vmlist){
    $myVM = Get-VM -Name $vm
    $snapshots = Get-SnapShot -VM $myVM
    if ($snapshots.Name.Length -ige 1 -or $snapshots.length){
    ForEach ($snapshot in $snapshots){
    $myObj = “” | Select-Object VM, Snapshot, Created, Description
    $myObj.VM = $$myVM.name
    $myObj.Snapshot = $snapshot.name
    $myObj.Created = $snapshot.created
    $myObj.Description = $snapshot.description
    $myCol += $myObj

    Comment by A. Mikkelsen — Friday, February 1, 2013 @ 19:37 pm

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress