Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Some friends here on the Hyper-V team shared a PowerShell 2.0 script for deleting a VM snapshot:
However BEFORE you go deleting snapshots, please read the Hyper-V Snapshot FAQ, and have a look at the Ben Armstrong Snapshot FAQ Video.
# Delete Virtual System Snapshot
param(
[string]$vmName = $(throw "Must specify virtual machine name"),
[string]$vmSnapName = $(throw "Must specify snapshot name")
)# Get the virtual machine by name
$vm = gwmi -namespace root\virtualization Msvm_ComputerSystem -filter "ElementName='$vmName'"# Get the setting data for the snapshot by name
$snapshot = get-wmiobject -namespace root\virtualization Msvm_VirtualSystemSettingData `
-filter "SystemName='$($vm.Name)' and SettingType = 5 and ElementName = '$vmSnapName'”
if($snapshot -eq $null -or $snapshot -is [array]){
throw "Unable to find single snapshot with name `"$vmSnapName`""
}
# Get the management service and apply the snapshot
$vmms = gwmi -namespace root\virtualization Msvm_VirtualSystemManagementService$result = $vmms.RemoveVirtualSystemSnapshot($snapshot)
if($result.ReturnValue -eq 4096){
# A Job was started, and can be tracked using its Msvm_Job instance
$job = [wmi]$result.Job
# Wait for job to finish
while($job.jobstate -lt 7){$job.get()}
# Return the Job's error code
return $job.ErrorCode
}
# Otherwise, the method completed
return $result.ReturnValue
For more info on how to use PS cmdlets see: https://www.microsoft.com/technet/scriptcenter/topics/msh/cmdlets/index.mspx
See also James O’Neil’s New and improved PowerShell Library for Hyper-V. Now with more functions and... documentation!
For all 35 sample Hyper-V PS1 scripts in a zipfile, go to: Hyper-V%20PowerShell%20Example%20Scripts.zip-download