Tags are a useful way to keep track of who owns what resource in a company wide Azure subscription.
You can enforce the creation of tags via the Apply tag and its default value policy you can assign to all or part of your subscription. But this only works for future resource created. What if you already have a lot there?
We can create the tags we need at the resource group level with this:
Get-AzureRmResourceGroup | ForEach-Object { Set-AzureRmResourceGroup -Name $_.ResourceGroupName -Tag @{ Key="value" } }
This will set the tag for all resource groups.
Then to copy the tags at the resource groups and assign them to the resources within the groups you can run the following:
$groups = Get-AzureRmResourceGroupforeach ($group in $groups){$resources = $group | Find-AzureRmResourceWrite-Host "Processing resource group $($group.ResourceGroupName)" -f Cyanforeach ($r in $resources){$resourcetags = (Get-AzureRmResource -ResourceId $r.ResourceId).Tagsforeach ($key in $group.Tags.Keys){if (($resourcetags) -AND ($resourcetags.ContainsKey($key))) { $resourcetags.Remove($key) }}$resourcetags += $group.TagsSet-AzureRmResource -Tag $resourcetags -ResourceId $r.ResourceId -Force}Write-Host}