How To Delete An Azure VM Resources Using Powershell? With 1 Best Example

In this comprehensive guide, we will walk you through the steps to delete an Azure Virtual Machine (VM) using PowerShell. Azure, Microsoft’s cloud computing platform, offers a wide array of services, and VMs are one of the fundamental building blocks for many applications and workloads. Learning how to delete a VM using PowerShell is essential for efficient resource management and cost optimization within your Azure environment.

How To Delete An Azure VM Resources Using Powershell?

How to obtain the Azure Virtual Machine object

To make things simpler for this script’s deletion of an Azure VM, first, obtain the VM object that houses all of the data we require. 

Use the command Get-AzVm. 

$vm = Get-AzVm -Name WINSRV19 -ResourceGroupName MyTestVMs
$vm = Get-AzVm -Name WINSRV19 -ResourceGroupName MyTestVMs

As you move through the script, you should be able to read different items from the $vm variable because the VM object should now be saved there.

Why Use PowerShell to Delete Azure VM?

PowerShell is a powerful command-line tool and scripting language that allows you to automate and manage various tasks in Microsoft Azure. When it comes to VM management, using PowerShell provides several advantages:

  • Efficiency: PowerShell allows you to delete VMs in bulk, saving time and effort compared to the Azure portal.
  • Automation: You can create scripts to automate the process of VM deletion, making it easy to manage a large number of VMs simultaneously.
  • Flexibility: PowerShell provides granular control over the deletion process, allowing you to customize actions as per your specific requirements.

Now, let’s dive into the step-by-step process of deleting an Azure VM using PowerShell.

Install Azure PowerShell Module

Before you can start using PowerShell to manage Azure resources, you need to install the Azure PowerShell module. Here’s how:

1: Check if Azure PowerShell is Installed

Open your PowerShell command prompt and type the following command:

Get-Module -ListAvailable Az
Get-Module -ListAvailable Az

This command will check if the Azure PowerShell module is already installed on your system.

2: Install Azure PowerShell

If Azure PowerShell is not installed, you can install it by executing the following command:

Install-Module -Name Az -AllowClobber -Force
Install-Module -Name Az -AllowClobber -Force

This command will download and install the latest version of the Azure PowerShell module on your machine.

Connect to Azure

Now that you have the Azure PowerShell module installed, the next step is to connect to your Azure account.

1: Sign in to Your Azure Account

Execute the following command to initiate the sign-in process:


S sign-in window will appear where you need to enter your Azure credentials.

2: Select Your Azure Subscription

If you have multiple Azure subscriptions associated with your account, you need to select the one you want to work with. Use the following command to list all available subscriptions:


Then, set the desired subscription using the following command:

Select-AzSubscription -SubscriptionName "Your Subscription Name"

3: Identify the VM to Delete

Before proceeding with the deletion process, you must identify the VM you want to delete. You can use the Get-AzVM cmdlet to list all VMs within your selected subscription:


This command will display a list of all VMs in your subscription, including their names, resource groups, and other important information.

4: Delete the Azure VM

Once you have identified the VM you wish to delete, you can proceed with the actual deletion using the Remove-AzVM cmdlet.

Before you delete the VM, it’s essential to confirm your intentions. Deleting a VM will remove all associated data, including virtual hard disks and networking configurations. To confirm the deletion, use the -Force parameter in the following command:

Remove-AzVM -ResourceGroupName "Your Resource Group Name" -Name "Your VM Name" -Force
Delete the Azure VM

Delete the VM: Use the Remove-AzVM cmdlet to delete the VM. The -Force parameter will skip the confirmation prompt during the deletion process.

Confirm the deletion (optional): If you prefer to see the confirmation prompt before deletion, you can omit the -Force parameter. Once the execution is completed, the VM and its associated resources (such as OS disk, NIC, etc.) will be deleted from your Azure account.

Always be cautious when running scripts to delete resources, as the action is irreversible and can lead to data loss. Double-check the resource names before executing the script, and ensure you have appropriate permissions to delete the VM.

Eliminate Public IP Addresses And Network Interfaces

Single or more NICs that were formerly connected to that VM. We may loop through each ID using the NetworkInterfaces field on the VM object, and remove the NIC using the Remove-AzNetworkInterface command.

You may also find out if a NIC is associated with a public IP address by looking at the IpConfiguration property on each NIC. If so, using the Remove-AzPublicIpAddress command, we can get rid of it.

Identify every NIC, disable it, search for public IP addresses on every NIC, and, if any are discovered, parse the PublicIpAddress property’s ID to get the name of the public IP address resource and disable it.

foreach($nicUri in $vm.NetworkProfile.NetworkInterfaces.Id) {
    $nic = Get-AzNetworkInterface -ResourceGroupName $vm.ResourceGroupName -Name $nicUri.Split('/')[-1]
    Remove-AzNetworkInterface -Name $nic.Name -ResourceGroupName $vm.ResourceGroupName -Force

    foreach($ipConfig in $nic.IpConfigurations) {
        if($ipConfig.PublicIpAddress -ne $null) {
            Remove-AzPublicIpAddress -ResourceGroupName $vm.ResourceGroupName -Name $ipConfig.PublicIpAddress.Id.Split('/')[-1] -Force
Eliminate Public IP Addresses And Network Interfaces

Take off the OS Disk

The OS disk is not taken out when deleting an Azure VM. The Remove-AzStorageBlob command can be used to delete storage blobs, including the OS disk, but first, as with the previous stages, you must conduct some research to identify the proper parameter values to supply.

Look for the OS disk storage container’s name. Once I realize this, I give it along to Remove-AzStorageBlob along with the storage account that the OS disk storage container is stored on in order to have the OS disk deleted.

$osDiskUri = $vm.StorageProfile.OSDisk.Vhd.Uri
$osDiskContainerName = $osDiskUri.Split('/')[-2]
$osDiskStorageAcct = Get-AzStorageAccount | where { $_.StorageAccountName -eq $osDiskUri.Split('/')[2].Split('.')[0] }
$osDiskStorageAcct | Remove-AzStorageBlob -Container $osDiskContainerName -Blob $o
Take off the OS Disk