-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_remove-all-vms.ps1
More file actions
37 lines (30 loc) · 1.18 KB
/
Copy path_remove-all-vms.ps1
File metadata and controls
37 lines (30 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#Requires -RunAsAdministrator
#Requires -Modules Hyper-V
# Remove ALL Hyper-V VMs (stop first if running, then remove VM and VHDX files)
$vms = Get-VM
if ($vms) {
foreach ($vm in $vms) {
Write-Host "Processing VM: $($vm.Name) (State: $($vm.State))" -ForegroundColor Yellow
# Stop if running
if ($vm.State -eq 'Running' -or $vm.State -eq 'Paused') {
Write-Host " Stopping VM..." -ForegroundColor Cyan
Stop-VM -Name $vm.Name -Force -TurnOff
}
# Get VHD paths before removing VM
$vhds = Get-VMHardDiskDrive -VMName $vm.Name | Select-Object -ExpandProperty Path
# Remove the VM
Write-Host " Removing VM..." -ForegroundColor Cyan
Remove-VM -Name $vm.Name -Force
# Remove associated VHDX files
foreach ($vhd in $vhds) {
if (Test-Path $vhd) {
Write-Host " Removing VHDX: $vhd" -ForegroundColor Cyan
Remove-Item -LiteralPath $vhd -Force
}
}
Write-Host " Done." -ForegroundColor Green
}
Write-Host "`nAll VMs removed." -ForegroundColor Green
} else {
Write-Host "No VMs found." -ForegroundColor Green
}