- Create and manage append blobs with PowerShell - Wed, Oct 12 2022
- Permanently delete a Key Vault in Azure using PowerShell - Fri, Feb 4 2022
- Restore Azure Files with PowerShell - Fri, Jan 28 2022
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 subscription | 800 |
Maximum resources per resource group (per resource type) | 800 |
Number of tags per resource or resource group | 15 |
Tag key length | 512 |
Tag value length | 512 |
Tags per subscription | Unlimited |
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
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.
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
With the next command, you can get all tag details for a resource.
(Get-AzureRmResource -ResourceName "TestNIC01" -ResourceGroupName "Onur_Tag_Test").Tags
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 }
Assigning a tag to a resource group is pretty similar:
Get-AzureRmResourceGroup "Onur_Tag_Test" | Set-AzureRmResourceGroup -Tag @{Company="Clouderz";Department="Automation"}
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 }
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"
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”
thank you for the article!
How to delete all resources with a tag?
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
Very useful. can you please help me on following usecase.
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.
looking for a script to inherit tags from Resource group to Resources
Hello,
How do you take the Subscription level Tags and a specific tags associated in all the subscriptions.
How to add single tag to multiple resources together(appservice, application insight , storage etc) using powershell command in azure
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