Listing snapshots–via PowerShell [Hyper-V]

Listing snapshots–via PowerShell [Hyper-V]

Rate This
  • Comments 2


After the relatively trick “taking a snapshot” script – today’s snapshot script is quite easy.  Listing the snapshots that a virtual machine has:

# Prompt for the Hyper-V Server to use
$HyperVServer = Read-Host "Specify the Hyper-V Server to use (enter '.' for the local computer)"
 
# Prompt for the virtual machine to use
$VMName = Read-Host "Specify the name of the virtual machine"
 
# Get the management service
$VMMS = gwmi -namespace root\virtualization Msvm_VirtualSystemManagementService -computername $HyperVServer
 
# Get the virtual machine object
$VM = gwmi MSVM_ComputerSystem -filter "ElementName='$VMName'" -namespace "root\virtualization" -computername $HyperVServer
 
# Get snapshot objects associated with the virtual machine
$Snapshots = gwmi -Namespace root\virtualization -Query "Associators Of {$VM} Where AssocClass=Msvm_ElementSettingData ResultClass=Msvm_VirtualSystemSettingData" 
 
write-host
write-host $VMName "has the following snapshots:" 
 
# Display a formatted list of snapshots
$Snapshots | sort CreationTime | Format-Table -Property @{n='Snapshot Name';e={$_.ElementName}}

One thing to point out here is that I am using a WMI query that looks at the association class (MSVM_ElementSettingData) as well as the result class (MSVM_VirtualSystemSettingData).  The reason why I do this is that if you grab all MSVM_VirtualSystemSettingData classes that are associated with a virtual machine – you will get two extra entries: one for the active virtual machine, and one for the latest snapshot (which results in one of your snapshots being duplicated).  The method that I am using here ensures that you only get snapshots, and you do not get any duplicates.

Cheers,
Ben

Attachment: ListSnapshots.zip
Leave a Comment
  • Please add 6 and 2 and type the answer here:
  • Post
  • This is nice, but I'm looking for something to list the Snapshot Name and its GUID.  Can this be done via this method?

  • Hi Ben, I am looking for to populate snapshot details in BGinfo (computer background utility which displays computer details) is that possible ?

Page 1 of 1 (2 items)