Home Azure Add Resource Tags to Azure Virtual Machines using Powershell

Add Resource Tags to Azure Virtual Machines using Powershell

by admin

Just a quick post on how to add resource tags to your Azure virtual machines using Powershell. Resource tags can be incredibly useful when managing Azure resources – Tags are name/value pairs that enable you to categorize resources and view consolidated billing by applying the same tag to multiple resources and resource groups. For example, you can use a ‘Dept’ tag to group together virtual machines that belong to the same department.

Now, whilst tags can be added to resources in the Azure Portal, this can be time consuming. If you have a need to add many tags quickly and to multiple resources, then Powershell is a great option. Below is a quick example on how to add a bunch of resource tags to a virtual machine.

Assuming you are authenticated to Azure, and have the Azure cmdlets available, either locally or using Azure Cloudshell, the following code can be used.

First of all, specify which VM, or group of VMs, you want to add tags to. I’ve done this below by creating a $vm variable.

$vm = Get-AzureRmResource -ResourceName mytestvm -ResourceGroupName myresourcegroup

Now we can set the tags using the Set-AzureRMResource cmdlet. In the example below, I’m adding ‘Dept’ and ‘Environment’ tags and their values.

Set-AzureRmResource -Tag @{ Dept="IT"; Environment="Test"; CostCenter="000000001" } -ResourceId $r.ResourceId -Force

Checking the VM in Azure Portal shows that the tags have been added:

AzureTags

Note that if you run this against a resource that already has tags, they will be overwritten with those set here. This method doesn’t append tags to the resource, instead it sets the resources tags exactly as they are specified in the command.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More