Tagging Microsoft Azure Resources Using Powershell (Az)


In Azure Cloud, Tags play a major role to manage resources in an easy way, in an other words Tags are an additional meta data associated with the Azure resources. We can assign the tags to the individual resources like VM, Storage Account, VNet and etc., and we can also assign the tags to the Resource Groups as well. Resource groups allow us to organize the related resources together and facilitate the management, but tags are used to group the resources beyond the resource groups including the resource groups, and at the same time resources inside the resource group do not inherit the tags associated with the resource group. Tags are Key and Value combination that can be assigned to the resources in the Azure cloud, for example…

Tag Key Tag Value
ResourceType VM
Project MyProject
Department Marketing
Environment Production
CostCenterCode 123456

Do bear in mind that each individual resource can have up to 15 tags max (Microsoft keeps updating the numbers time to time, so please refer the Microsoft Docs for the exact number), and ensure the tags are unique and consistent naming convention among Azure resources.

Tags are used to organize the deployed resources in the Azure cloud, we could search the resources by tag key/value, for example search the resources with the tags associated {Key:Value} Type:VM and Environment:Production, then the search results all the production VMs across the resource groups within a subscription. Tags are also used to view the related resources, like all the resources tagged to a specific project or a specific cost center and to facilitate the billing and cost management.

Tags can be created at the time of creating resources or at the later time by using the Azure portal or any command line tools like PowerShell or Azure CLI.

Let’s see how we can create and manage the tags using PowerShell…

#requires -Module Az
# Connect-AzAccount
### Add new tags to an existing resource
# Get the resource
$Resource = Get-AzResource -Name testvm -ResourceGroupName test-rg
 
# Resource tags
[hashtable] $Tags = $Resource.Tags
 
# Ensure not to overwrite the tags
if ($null -eq $Tags) 
{
    [hashtable] $Tags = @{Type="VM"; Environment="Test"} 
}
else 
{
    $Tags += @{Type="VM"; Environment="Test"} 
}
 
# Add new tags to the resource (-Force to override the confirmation if there are any existing tags)
Set-AzResource -ResourceId $Resource.Id -Tag $Tags -Force
 
### Remove an existing tag / remove all tags from a resource
 
# Get the resource
$Resource = Get-AzResource -Name testvm -ResourceGroupName test-rg
 
# Resource tags
[hashtable] $Tags = $Resource.Tags
 
# Remove the specific tag
$Tags.Remove("Type")
 
# Overwrite the remaining tags to the resource (-Force to override the confirmation if there are any existing tags)
Set-AzResource -ResourceId $Resource.Id -Tag $Tags -Force
 
## Remove all tags
Set-AzResource -ResourceId $Resource.Id -Tag @{} -Force
 
### List all the resources with a specific tag key
Get-AzResource -TagName "Environment"
 
### List all the resources with a specific tag value
Get-AzResource -TagValue "Test"
 
### List all the resources with a specific tag key and value
Get-AzResource -Tag @{Environment="Test"}
 
### List all the tags and number of resources associated in a subscription.
Get-AzTag

Share it on     |   |   |   | 
  Prev:  

Create And Assign A Public IP To An Azure Virtual Machine

  :Next  

Create New Azure VM Using PowerShell(Az)

comments powered by Disqus