When it comes to managing hundreds of resources in Azure, resource tags are key instruments to group, organize, and report the resources with PowerShell.
Latest posts by Baki Onur Okutucu (see all)

In Azure, resource groups let you group and manage resources. Working with resources groups lets us perform some mass operations such as permission control and activity log management. However, it is a big advantage to use resource tags to organize resources. Apply resource tags to resources or resource groups to group them logically.

We can then easily identify and manage resources with tags through PowerShell for automated tasks. Tags also allow us to know which department is using which resources. For example, you could assign a tag named "Finance" to some resources. This way, we can manage all resources belonging to the finance department regardless of which resource group they are in.

The following table shows the default quotas for resources in Azure.

Maximum resource groups per subscription800
Maximum resources per resource group (per resource type)800
Number of tags per resource or resource group15
Tag key length512
Tag value length512
Tags per subscriptionUnlimited

Creating tags using PowerShell ^

Creating an Azure resource tag is pretty simple. Each tag requires two properties: keys (names) and values.

New-AzureRmTag -Name Company -Value Clouderz
New-AzureRmTag -Name Department -Value Automation
New-AzureRmTag -Name ManagedBy -Value Onur

Get-AzureRmTag -Detailed | select name,values
Listing tags

Listing tags

You can remove tags with this command:

Remove-AzureRmTag -Name Company

Assigning tags to resources or resource groups ^

After you create a tag, it becomes available in the Azure Portal. There you can assign it to a resource or a resource group.

Tags in Azure Portal

Tags in Azure Portal

It is important to note that resource tags are not inherited. So applying a tag to a resource group does not automatically apply the resources in that resource group with the tag. If we need to apply the tags to all resources in a resource group, we can either manually apply it for an individual resource or automate the task with PowerShell for multiple resources.

You can execute the following commands to apply a tag to a single resource:

Get-AzureRmResource -ResourceName "TestNIC01" -ResourceGroupName "Onur_Tag_Test" | Set-AzureRmResource -Tag @{Company="Clouderz";Department="Automation"} -Force
Applying a tag to a resource

Applying a tag to a resource

With the next command, you can get all tag details for a resource.

(Get-AzureRmResource -ResourceName "TestNIC01" -ResourceGroupName "Onur_Tag_Test").Tags
Getting a resource with its tag details

Getting a resource with its tag details

We can use various combinations to manage multiple resources using resource tags with PowerShell. One of the most used scenarios is to get a list of resources tagged with a specific value. In the example below, we will get all resources with a tag named "Company" and a value of "Clouderz." You can customize this code to list resources used by a specific project or a team.

Find-AzureRmResource -TagName "Company" -TagValue "Clouderz" | foreach {
	Get-AzureRmResource -ResourceId $_.resourceid | select name, tags
}
Listing resources with specific tags

Listing resources with specific tags

Assigning a tag to a resource group is pretty similar:

Get-AzureRmResourceGroup "Onur_Tag_Test" | Set-AzureRmResourceGroup -Tag @{Company="Clouderz";Department="Automation"}
Applying tags to a resource group

Applying tags to a resource group

It is also possible to assign the same tags for a resource group to all resources within it. In a way, this gives you an inheritance feature for tagged resource groups.

$resourceGroupName="Onur_Tag_Test"
$tags_to_apply=(Get-AzureRmResourceGroup -Name $resourceGroupName).tags
Find-AzureRmResource -ResourceGroupName $resourceGroupName | foreach {
	Set-AzureRmResource -ResourceId $_.resourceid -Tag $tags_to_apply -Force
}
Applying tags to multiple resources

Applying tags to multiple resources

Listing all tagged resources ^

Lastly, to get a complete list of resources in a subscription you have tagged, you can use the code below.

Subscribe to 4sysops newsletter!

$result=@()
$tagkeys=Get-AzureRmTag
foreach($tagkey in $tagkeys) {
	$tagvalues=(Get-AzureRmTag $tagkey.name).values
	foreach($tagvalue in $tagvalues) {
		$result+=Find-AzureRmResource -tag @{$tagkey.name=$tagvalue.name} | select name,resourcegroupname,location,@{label="tagName";expression={$tagkey.name}},@{label="tagValue";expression={$tagvalue.name}}
	}
}
$result | Out-GridView -Title "Azure Resources that have been assigned with tags"
Reporting all tagged resources

Reporting all tagged resources

9 Comments
  1. ace 5 years ago

    Excellent for: can you add tag owner please

    $result=@()
    $tagkeys=Get-AzureRmTag
    foreach($tagkey in $tagkeys) {
        $tagvalues=(Get-AzureRmTag $tagkey.name).values
        foreach($tagvalue in $tagvalues) {
            $result+=Find-AzureRmResource -tag @{$tagkey.name=$tagvalue.name} | select name,resourcegroupname,location,@{label=“tagName”;expression={$tagkey.name}},@{label=“tagValue”;expression={$tagvalue.name}}
        }
    }
    $result | Out-GridView -Title “Azure Resources that have been assigned with tags”

  2. Antyp 4 years ago

    thank you for the article!

    How to delete all resources with a tag?

  3. Author
    Baki Onur Okutucu 4 years ago

    Hi Antyp,

    The following code can be used for what you were looking for

    $rg= “resourcegroupname”
    $tagname = “tagname”
    $TagValue =”tagvalue”
    get-AzureRmResource -TagName $tagname -TagValue $TagValue | Remove-AzureRmResource -force

  4. Prasad Chundi 4 years ago

    Very useful. can you please help me on following usecase.

    1. query all resources in a subscription with / without tags in csv format.
    2. query all resources in a subscription that do not have specify tag name in CSV format and then apply specific tag name and value to all those resources and generate a new csv output.

     

    • Kalyan 4 days ago

      I saw there are some workbooks on Github, by those you can prepare CSV reports for the resources which have a tag and don’t have a tag.

  5. Sandeep 2 years ago

    looking for a script to inherit tags from Resource group to Resources

  6. Arjun 2 years ago

    Hello,

    How do you take the Subscription level Tags and a specific tags associated in all the subscriptions. 

  7. Pranali 2 years ago

    How to add single tag to multiple resources together(appservice, application insight , storage etc) using powershell command in azure

  8. Agnel 2 years ago

    Hi I am looking for Some Help Here.

    I want to Update Tags to WVDs with assigned User Name. I have some Polishing for the assigned user name and then update the Tag with that Polished Assigned User. The tricky part is WVDs are spread across multiple Subscriptions and Tags can be only pulled with Get-AZresource

    My Logic.
    1.Get-azWVDsession host to get all session host and Assigned user
    2. User splitters to polish the hostname and Assigned user
    3.Put the data in Hash Table
    4. Collect all the VM data across all suscription
    5. Run for each loop and see if $Hashtable.keys matches with VM if yes then Get Tags and ResourceID
    6. Now tricky part how I can update the Tags with $Hashtable.Values accoridingly

Leave a reply

Your email address will not be published.

*

© 4sysops 2006 - 2022

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account