How to apply Tags on all Azure resources using PowerShell

You can use the tags to logically organize all the resources in Azure. Tag in azure is a name and value pair which can be applied using GUI or by using PowerShell. You can take the approach of GUI if there are not too many resources where you have to manage the tags. In that case, you can click on each resource and then click Tags (as shown in below screenshot) on the left hand side pane and fill the details for Name/Value Pair.

While this method is manual and also requires a lot of time if the number of resources where the tags needs to be populated increases from lets say 10 to 100 or 500 or 5000 etc. Best practices is to apply the resource tags at the time of the resource creation so that each resource is Tagged as per business requirement. Its very easy to forget applying the tags on the resources. To make sure administrators or users who are creating the resources must apply tags, Use Azure Policy and apply it to the Root Management Group which contains your subscription(s) or create different Azure Policy for different subscriptions as per the requirement. This will ensure that the admins or users apply tags to the resources otherwise the the resource creation will fail at the Validation step.

As you now know how to apply the tags on the resources using GUI. Lets see how the tags can be applied to the azure resources using PowerShell. Before we are able to run the commands which apply the tags or remove the tags from the resource, you will have to install Azure PowerShell module. Its recommend you use the latest version of PowerShell for installing Azure PowerShell module. To check the current version of PowerShell on your workstation, launch PowerShell and type $PSVersionTable.PSVersion.

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

Once you have installed the Azure PowerShell Module, you can now manage the azure resource tags (make sure you have the required permission to manage the Tags)

It makes it easy when you have same set of tags which need to be applied to all resources in a resource group. Below is the PowerShell code you can run to update the Resource Tag on all azure resources within an azure resource group. Please note when we use the cmdlet update-AzTag with Merge, if tags are already existing on any of the resource, the new set of tags will get appended and will not overwrite the existing tags. Existing resource tags will stay as it is.

$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 $ -Tag $tags -operation Merge

0 0 vote
Article Rating
Notify of
Inline Feedbacks
View all comments