How to get the applied azure resource tags using PowerShell?

PowerShellMicrosoft TechnologiesSoftware & Coding

To get all the applied tags to the Azure resources we need to use the Get-AZTag command and need to provide ResourceID to it. For example,

We need to retrieve the Azure VM tags and we will use its resource ID.

PS C:\> $vm = Get-AzVM -Name Testmachine2k16
PS C:\> Get-AzTag -ResourceId $vm.Id

You can see the output in the properties window. Another simple method is to use the Tags property for that particular cmdlet. For example, Get-AzVM, Get-AZResourceGroup, etc use the tag property for displaying the applied tags.

PS C:\> Get-AzVM -VMName TestMachine2k16 | Select -ExpandProperty Tags
Key             Value
---             -----
Owner           Chirag
For             Ansible
Patching_Day    Sunday
Application     SecretTag

Similarly, to get the resource group tags, you can use either the Get-AZTag command with the ResourceID property of the resource group or the Get-AZResourceGroup command with the Tag property.

PS C:\> $rg = Get-AzResourceGroup -ResourceGroupName TestRG
PS C:\> Get-AzTag -ResourceId $rg.ResourceId


PS C:\> Get-AzResourceGroup -Name TestRG | Select -ExpandProperty Tags

To search for the specific tags. We need to use -Name property of the Get-AZTag command. For example, we need to search the tag called Patching_Day then we can use the below command. It shows the Patching_Day tag from the entire subscription because we haven’t provided any specific resource or resource group.

The count property shows the number of times the tag is applied to the resources and ValuesTable and values property shows the values associated with that tag key.

PS C:\> Get-AzTag -Name Patching_Day | fl

Name        : Patching_Day
ValuesTable :
  Name    Count
  ======  =====
  Sunday  2

Count       : 2
Values      : {Sunday}
Published on 06-Apr-2021 11:26:09