Completely delete Hyper-V VMs

This is a powerful PowerShell command that completely deletes VMs. It stops the VM, remove it from Hyper-V and deletes configuration files and virtual disks associated.

To remove a single VM run:
[sourcecode language=”powershell”]
Get-VM -ComputerName hypervhost -Name testvm01 | %{ Stop-VM -VM $_ -Force; Remove-VM -vm $_ -Force ; Remove-Item -Path $_.Path -Recurse -Force}
[/sourcecode]
To delete many VMs at once, create a txt list like this:

name
testvm01
testvm02
testvm03

and run:
[sourcecode language=”powershell”]
Import-Csv C:list.txt | foreach { Get-VM -ComputerName hypervhost -Name $_.name } | %{ Stop-VM -VM $_ -Force; Remove-VM -vm $_ -Force ; Remove-Item -Path $_.Path -Recurse -Force}
[/sourcecode]
To delete ALL VMs of a hyper-V host run:
[sourcecode language=”powershell”]
Get-VM -ComputerName hypervhost | %{Stop-VM $_ -Confirm:$false; Remove-VM $_ -DeletePermanently -Confirm:$false
[/sourcecode]
 

Source: http://vniklas.djungeln.se/2012/06/13/lets-remove-some-vm%C2%B4s-with-powershell-on-hyper-v-3-in-windows-2012/

Share

One comment

Comments are closed.