Fredrik Johansson I understand your concern and frustration regarding the time-consuming nature of updating tags on resources, especially across multiple subscriptions.
Using PowerShell scripts, we can automate the task of updating tags for resources, eliminating the need for manual, resource-by-resource updates. I have provided you a sample solution on how it can be done for a resource. If the case is for multiple subscriptions and multiple resources, the solution needs to be modified based on the requirement.
On your confusion about tag name or Tag Key: - Tags, also referred to as key-value pairs or name-value pairs. These cannot be modified in a partial manner.
Here is draft version of code you requested. Update it with appropriate values and test it out on a subscription to check if it fits your requirement.
Import-Module -Name Az.Resources
# Get all subscriptions in the Azure account
try {
"Logging in to Azure..."
Connect-AzAccount
}
catch {
Write-Error -Message $_.Exception
throw $_.Exception
}
$subscriptions = Get-AzSubscription
# Loop through each subscription
foreach ($subscription in $subscriptions) {
# Set the current subscription
Set-AzContext -SubscriptionId $subscription.SubscriptionId
# Get all resource groups for the subscription
$resourceGroups = Get-AzResourceGroup
$tagName = "Region"
$newTagName = "Country"
# Loop through each resource provider
foreach ($rg in $resourceGroups) {
$resources = Get-AzResource -ResourceGroupName $rg.ResourceGroupName
foreach ($r in $resources) {
$resourceId = Get-AzResource -ResourceId $r.ResourceId
if ($resourceId.Tags.ContainsKey("Region")) {
$tagValue = $resourceId.Tags[$tagName]
$resourceId.Tags.Remove($tagName)
$resourceId.Tags.Add($newTagName, $tagValue)
# Update the resource with the new tags
$resourceId | Set-AzResource -Force
}
}
}
}