Search code examples
azurepowershellcommand-line-interfaceazure-powershelltagging

How to bulk update a particular Tag in Azure on the basis of Subscription, Region and another tag?


I need to bulk update a particular tag which is applied on a variety of resources in Azure like VMs, NSGs, AGWs, RGs, so on and so forth. I need to update this tag for all kind of resources on which they are applied. How can I do the same ? I need to filter resources on basis of Subscription, Region and another tag value ? Can I use AZ CLI or Azure Powershell and if yes, how ? How can I put filter of Subscription, Region and another tag value for bulk tag update ?


Solution

  • To filter with subscription, you need to use Set-AzContext to set the specific subscription, because azure powershell can just run against one subscription, to see the subscriptions that your logged account can access, you could use Get-AzSubscription.

    Then you can filter with region and another tag value, after getting them, update their tags via Update-AzTag.

    Connect-AzAccount
    Set-AzContext -Subscription "<subscription-id>"
    $resources = Get-AzResource -TagName "tag1" -TagValue "value1" | Where-Object {$_.Location -eq 'centralus'}
    $Tags = @{"tag1"="value1"; "tag2"="value2";}
    $resources | ForEach-Object {
        Update-AzTag -ResourceId $_.ResourceId -Tag $Tags -Operation Merge
    }
    

    enter image description here