Skip to content

Instantly share code, notes, and snippets.

@keithga
Created August 30, 2023 03:55
Show Gist options
  • Save keithga/4afac6a8ebfa91512b7f33350c9ee0ca to your computer and use it in GitHub Desktop.
Save keithga/4afac6a8ebfa91512b7f33350c9ee0ca to your computer and use it in GitHub Desktop.
# This script will remove the VM *AND* Vhd
$VMs = get-vm | Out-GridView -OutputMode Multiple
$Disks = $VMs | Get-VMHardDiskDrive | % path
$VMs | stop-vm -Force -TurnOff -Confirm:$False -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
$VMs | Remove-VM -Force -Confirm:$False
$disks | Write-Verbose
while ( test-path $Disks ) {
try {
remove-item $Disks -Force
}
catch {
write-host "Retry Deletion..."
start-sleep 10
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment