Add Tags to Azure resources using PowerShell

Tags in Azure provide a way to logically structure and categorize your resources. A tag in Azure consists of a name and value pair, and you can apply them either through the Azure portal or PowerShell.

If you have a limited number of resources to manage, using the Azure admin portal approach is suitable. You can access this by selecting each resource, then clicking on ‘Tags‘ in the left-hand pane, and entering the details for the Name/Value Pair, as illustrated in the screenshot below.

Add Tags to Azure resources using PowerShell
Add Tags to Azure resources using PowerShell

it’s best practice to apply resource tags during the resource creation process. This ensures that each resource is tagged according to business requirements. While the manual method can be time-consuming, especially as the number of resources requiring tags increases,

It’s easy to overlook tagging resources, which is why using Azure Policy is recommended. This approach enforces the addition of tags during resource creation, preventing the resource creation process from proceeding if the required tags are not applied during the validation step.

Steps to Add Tags using Powershell

Use the below steps to add tags on Azure resources using Powershell. First, Install the Azure Powershell module on your device.

Install Azure Powershell Module

if ($PSVersionTable.PSEdition -eq 'Desktop' -and (Get-Module -Name AzureRM -ListAvailable)) {
    Write-Warning -Message ('Az module not installed. Having both the AzureRM and ' +
      'Az modules installed at the same time is not supported.')
} else {
    Install-Module -Name Az -AllowClobber -Scope CurrentUser
}
Steps to Add Tags using Powershell
Steps to Add Tags using Powershell

Here is the PowerShell code you can execute to update the resource tags on all Azure resources within an Azure resource group. Keep in mind that when using the ‘Update-AzTag‘ cmdlet with the ‘Merge‘ option, any existing tags on a resource will not be overwritten. Instead, the new set of tags will be appended, preserving the existing tags.

  • $tags – Provide the tags you want to append to the existing tags on a resource.
  • $resourcegroupname – When you execute below powershell code, it will prompt for a resource group name on the console. Provide a resource group name. The script will go through all resources within this resource group and add the given tags.
  • $resources – This will get All Azure resources in a resource group.
$tags = @{"Application"="IT"; "Department"="IT" ; "Environment"="Production"}
$resourcegroupname = Read-Host -Prompt 'Enter a Resource Group Name'
$resources = Get-AzResource -ResourceGroup $resourcegroupname
foreach ($Resource in $Resources){
Write-Host "Processing "$resource"" -ForegroundColor Black -BackgroundColor white
Update-AzTag -ResourceId $resource.id -Tag $tags -operation Merge
}
Steps to Add Tags using Powershell
Steps to Add Tags using Powershell
Steps to Add Tags using Powershell
Steps to Add Tags using Powershell

Leave a Comment

Discover more from TechPress

Subscribe now to keep reading and get access to the full archive.

Continue reading