Tag Azure Virtual Machine With PowerShell

In this blog post, I will show you how I tag an Azure virtual machine using Azure PowerShell.

Azure PowerShell

Microsoft Azure PowerShell allows us to manage Azure resources with the same look and feel PowerShell language and syntax.


In this demonstration, I will show you how I do the following few things:

  • Tag and an Azure VM with a new tag
  • Add a tag to an existing tags
  • Remove tags from an Azure

Get Azure PowerShell

Before you start, make sure you have Azure PowerShell up and running.

Get VM

Let’s start with getting the details of the VM using the following cmdlet.

#Get VM
$vm = Get-AzResource -Name centos -ResourceType Microsoft.Compute/virtualMachines

Add new Tag

Now, I will add a tag to an Azure VM without existing tags using the code below:

#Add new Tag
Set-AzResource -ResourceId $vm.ResourceId -Tag @{Type="Linux"} -Force

View Tags

After running the cmdlet, I will check the tags.

#View Tags

Add a Tag to an existing Tags

To add a new tag to existing tags, I will run the following code.

#Add Tag to existing Tags
Set-AzResource -ResourceId $vm.ResourceId -Tag $new.tag -Force

Remove Tags

To remove all the tags from a VM I will run the following code:

#Remove Tag
Set-AzResource -ResourceId $vm.ResourceId -Tag @{} -Force