Delete Azure Virtual Machines Including NIC and Disk with PowerShell

# Load Azure PowerShell module
Import-Module -Name Az
# Read in the VM names and RG
$vmNames = "AVD1”, “AVD2”
$resourceGroup = "RG-AVD"
# Loop through the VM names and delete each VM, NIC, and disk
foreach ($vmName in $vmNames) {
    Write-Host "Deleting VM, NIC, and disk for $vmName..."
    # Get the NIC and disk IDs for the VM
    $vm = Get-AzVM -ResourceGroupName $resourceGroup -Name $vmName
    $nicName = $vm.NetworkProfile.NetworkInterfaces[0].Id.Split("/")[-1]
    $osDiskName = $vm.StorageProfile.OsDisk.Name
    # Delete the VM. Add -AsJob to speed up
    Remove-AzVM -ResourceGroupName $resourceGroup -Name $vmName -Force -whatif
    # Delete the NIC -AsJob to speed up
    Remove-AzNetworkInterface -Name $nicName -ResourceGroupName $resourceGroup -Force -Whatif
    # Delete the OS disk -AsJob to speed up
    Remove-AzDisk -Name $osDiskName -ResourceGroupName $resourceGroup -Force -whatif
Share or Save this: