Managing Tags in Azure with PowerShell

Tags in Azure provide the Azure admin a way to organize Azure resources logically, using user-defined properties.  They are also commonly used to organize resources for billing and management. Tags can be applied to entire resource groups or to resources directly, and using them allows you select resources or resource groups from the console, web portal, PowerShell, and even through the API. Tags

Each tag consists of a name and a value. For example, many admins will tag their “production” resources in Azure with a tag called "Environment" and a value of "Production".  Doing so allows for easy identification/grouping of production resources.

EDITOR'S NOTE:  This tutorial was excerpted from the complete online video course, "Creating and Managing Azure Virtual Machines with PowerShell", which can be found on the Udemy training platform.

By using tags, you can retrieve all related resources in an Azure subscription, based on the values of the tags.  Tags can be placed on Compute, Network, and Storage resources during creation as well as after resource creation, using PowerShell. This tutorial concentrates on viewing/editing tags placed on Virtual Machines.

How to Tag a VM with PowerShell

Tagging a virtual machine requires you to retrieve the virtual machine, load all existing tags into a variable, update the list with any new tags, and then to assign the newly-updated list of tags to the virtual machine.  This process is required because when you add a tag to a resource, the new addition overwrites any tags currently assigned to the resource.  By extracting the existing tags, updating them, and then reassigning the tags, you can be sure that no tags are lost.

To assign a new tag to a virtual machine, retrieve the Virtual Machine with the Get-AzureRmVm cmdlet.

Get-AzureRmVM -ResourceGroupName "VMLab" -Name "MyVM3"

If your Virtual Machine already contains tags, you will see existing tags assigned to your virtual machine.  If it has not been tagged, the tags field will be empty:

You use the Set-AzureRmResource command to add tags via PowerShell. Below is an example of how to add additional tags to a virtual machine, using PowerShell.

The first step to tagging a virtual machine is to store all existing tags assigned to it, in a variable.  Run the command below to store all existing tags for the MyVM3 virtual machine in a variable called $tags:

$tags = (Get-AzureRmResource -ResourceGroupName VMLab -ResourceType Microsoft.Compute/virtualMachines -Name MyVM3).Tags

Running the command below outputs whatever tags are stored:


In our case, there are no tags yet:

After pulling all existing tags into our $tags variable, we need to add our additional tag to the $tags variable, using the command below.

$tags += @{Name="Location";Value="MyLocation"}

The command above adds a tag called “Location” to the list of tags for MyVM3 stored in our variable.  It also sets the value of this tag to “MyLocation”.

The command below actually assigns the updated list of tags to the MyVM3 virtual machine.

Set-AzureRmResource -ResourceGroupName VMLab -Name MyVM3 -ResourceType "Microsoft.Compute/VirtualMachines" -Tag $tags

After updating the tags on MyVM3 with the command above, you can run the Get-AzureRmResource command below to confirm that the new “Location” tag has been added:

(Get-AzureRmResource -ResourceGroupName VMLab -ResourceType "Microsoft.Compute/VirtualMachines -Name MyVM3).Tags

You will sometimes find yourself in a position where you need to DELETE tags from virtual machines.  The Set-AzureRmResource command is used to delete tags.  Run the command below to delete all tags from the MyVM3 virtual machine.

Set-AzureRmResource -ResourceGroupName VMLab -Name MyVM3 -ResourceType "Microsoft.Compute/VirtualMachines" -Tag @{}

Confirm that all tags are gone for MyVM3 by running the command below:

(Get-AzureRmResource -ResourceGroupName VMLab -ResourceType "Microsoft.Compute/VirtualMachines -Name MyVM3).Tags

If you would like to perform the exercises covered in this tutorial, replace all instances of "VMLab" with the name of your own resource group. Also, replace all instances of "MyVM3" with the name of your own virtual machine that you want to tag.

More Learning Resources

If you’d like to learn more about topics like this one, visit me at my website or at my training site.  You can also find me on LinkedIn and on Facebook.  If you are after free video tutorials, visit my YouTube channel.  Some of my courses, like this one, can also be found on, where you can use coupon code THOMASMITCHELL at checkout to get any one of my complete online courses for just $12.99.

Click here to join the Understanding Azure Facebook group or here for the latest Azure practice questions, answers, explanations, and reference materials.

Thomas Mitchell

Tom is a 20+ year veteran of the IT industry and carries numerous Microsoft certifications, including the MCSE: Cloud Platform and Infrastructure certification. A Subject Matter Expert in Active Directory and Microsoft Exchange, Tom also possesses expert-level knowledge in several other IT disciplines, including Azure, Storage, and O365/Exchange Online. You can find Tom at his website, on LinkedIn, or on Facebook. Need to reach him by phone? Call 484-334-2790.