Assigning Tags to Azure Resources - PowerShell

We all heard a lot about Tags and this something is actually makes your job very easy specially at the enterprise level where there are lots of different teams and departments. In this post we would be assigning Tags to the existing VM with the help of Powershell.


These Tags gives metadata to Azure resources to logically organize them into a Taxonomy. Each Tag consist of Name and Value pair. After you apply tags , you can retrieve all the resources in your subscription with that Tag name.


Tag enables you to retrieve related resources from different resource groups. This approach is very helpful when you need to organize resources for billing or management.



Few Limitations

  • Not all resource type support Tags.
  • Each resource or resource group can have max. of 50 Tags
  • Tags applied to resource group are not inherited.
  • Tag cant be applied to classic resources.
  • User must have write access to apply tags on that resource  


In this scenario we have on VM as shown and no Tags applied and we would be applying tags to this resource with the help of PS.








Get-AzResource  This can help you get the resources and you can choose the name or resource that you are looking for in my case its resource group




$r = Get-AzResource -ResourceName "lab-vm" -ResourceGroupName "ARG"
 We are getting the resource info in a variable so that we can run the set command next easily 



Set-AzureRmResource -Tag @{ Dept="IT"; Environment="Test" } -ResourceId $r.ResourceId -Force
 In this command we are setting the Tags as array and we can give multiple values as array or by separating with semi colan ;



You will see the result as shown:





  Once you see its succeeded , you can refresh the resource and tag would appear as shown -





2 comments:

MS Defenders

 Microsoft Defender offers a wide range of security solutions, similar to the ones we've discussed (Defender for Containers, Defender fo...