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/
Pantelis Apostolidis is a Sr. Specialist, Azure at Microsoft and a former Microsoft Azure MVP. For the last 20 years, Pantelis has been involved to major cloud projects in Greece and abroad, helping companies to adopt and deploy cloud technologies, driving business value. He is entitled to a lot of Microsoft Expert Certifications, demonstrating his proven experience in delivering high quality solutions. He is an author, blogger and he is acting as a spokesperson for conferences, workshops and webinars. He is also an active member of several communities as a moderator in azureheads.gr and autoexec.gr. Follow him on Twitter @papostolidis.
Reblogged this on Alexander Lemonlatte.