Azure, PowerShell : Deleting a VM with PowerShell

We created a VM in this post. Now is time to delete that VM. Good times.

To start with lets get the required Azure modules added in. Open a PowerShell windows and do the following:

Install-Module az #Installs lots of packages, 
Install-module az.resource #but not this one.

Now we need to get authenticated.


Once you're in we can take a look at the VM's in our estate like this.


To get more information on a specific VM we can select it and pipe it to format list (fl) which which will show us everything the "Get-AZVm" returns.

get-azvm -Name Demo1 | fl

Now lets get down to deleting this VM. You will need the VM name, and the name of the resource group it is in. If you look in the expanded information set for the VM, I have highlighted the resource group. However another way to get this would be to list the resource groups


Then list the contents of the resource groups to show the VM you are looking to delete.

Get-AZVm -ResourceGroupName Demo

Now we can delete the VM with the following command. If you do not add "-force" to the command you will have to confirm the command with an onscreen prompt, which is not ideal for a script.

Remove-AzVM -name Demo1 -ResourceGroupName Demo -Force

And the VM is now deleted.


Something to look out for. When you created the VM (Especially if you did it from my example here) You probably have a public IP in your instance that was assigned to the VM and some network stuff. This will not be deleted by the previous process and is currently costing you upwards of $0.004/hour.

I will cover how to identify and delete that in next weeks exciting instalment of....