PowerShell: Remove Hyper-V VM Snapshots

Hyper-V’s snapshot feature is a double-edged sword. It’s a convenient method to make a quick backups of a machine state prior to certain activities. However, the presence of check-points would sometimes interfere with the VM’s migration (under certain conditions) and restoration (via means other than using checkpoints). Therefore, it’s necessary for Admins to perform systems maintenance by removing Hyper-V snapshots from all guest VM’s on a regular basis, especially if there are enterprise backup systems already available to serve the purpose of business continuity.

# purgeVmSnapshots.ps1


function purgeVmSnapshots([string[]]$vmName,[string]$snapshotLabel){
    # Usage:
    # a. purgeVmSnapshots $vmNames $snapShotLabel # This is to remove specific snapshot by a label
    # b. purgeVmSnapshots $vmNames # This will purse all snapshots matching VM names

    $vmClusterNodes=Get-ClusterGroup|?{$_.GroupType -eq 'VirtualMachine'}
    foreach ($vm in $vmName){
        $ownerNode=($vmClusterNodes|?{$vm -eq $_.Name}).OwnerNode
        if($ownerNode.State -eq 'Up'){
                        Remove-VMSnapshot -VMName $vm -Name $snapShotLabel -ea Stop
                        Remove-VMSnapshot -VMName $vm -ea Stop
                    return $true
                    Write-Error $_
                    return $false
            $isSnapshotRemoved=invoke-command -ComputerName $ownerNode.Name -ScriptBlock $removeSnapShotCommand -Args $vm,$snapShotLabel
            write-warning "$vm is currently owned by DOWNED node $($ownerNode.Name). No snapshots can be taken for such guest VM."
            write-warning "$vm is does not match any Hyper-V Host in this cluster $((get-cluster).Name)"
    return $results

#Checkpoint-VM -vmName $vmNames -SnapshotName $snapShotLabel
purgeVmSnapshots $vmNames $snapShotLabel

Leave a Reply

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