Force Tag all resources in a resource group to inherit Tag from Resource Group

If you have a bunch of Azure Resources that is already part of a Resource Group and you want to apply tag to all resources based on the tag policy, how do you do it?

Generally when you create a Tag policy in your subscription. You will have to apply that policy to a Resource Group. Now when you create a new resource and add that newly created resource into the resource group, the resource will get tagged based on the policy applied to the resource group previously. What what about for existing resources that is part of the resource group?
you have 2 options in this case

option – 1

You can move the resource out of resource group and move it back in for the resource group. This causes the policy to force apply.

option – 2

But what if you have lets say 400 resources inside that resource group? you probably not going to move individual resources in & out of the policy. In this scenario, you can follow below steps.

  1. Log in to Azure Portal. Search for & open your resource group.
  2. Apply previously created policy & tag value to the Resource Group
  3. now open powershell and log in to your tenancy
  4. Supply below code and press enter

    $resourceGroupName = ‘name of your resourcegroup’

    $azureRGInfo = Get-AzureRmResourceGroup -Name $resourceGroupName
    foreach ($item in $azureRGInfo)
    {
    Find-AzureRmResource -ResourceGroupNameEquals $item.ResourceGroupName | ForEach-Object {Set-AzureRmResource -ResourceId $PSItem.ResourceId -Tag $item.Tags -Force }
    }

Leave a Reply

Your email address will not be published. Required fields are marked *