'How to pull all tags at resource level in azure

I am Trying to pull All the Tags resource level in azure

$subscripionList = Get-AzSubscription 


foreach ($subscriptionId in $subscripionList) {
    Write-Host 'Getting details about SubscriptionId :' $subscriptionId
    Set-AzContext -Subscription $subscriptionId
    #Select-AzureRmSubscription -SubscriptionName $subscriptionId

    $resourcesgroups = Get-AzResourceGroup

    foreach($resourcesgroup in $resourcesgroups){

    Write-Host 'resourcegroup :' $resourcesgroup
   
    $resources = Get-AzResource -ResourceGroupName $resourcesgroup.ResourceGroupName
       #$azure_resources = Get-AzResource 


    foreach($resource in $resources){

    Write-Host $resource
{
    #Fetching Tags
    $Tags = $resource.Tags
    
    #Checkign if tags is null or have value
    if($Tags -ne $null)
    {
        foreach($Tag in $Tags)
        {
            $TagsAsString += $Tag.Name + ":" + $Tag.Value + ";"
        }
    }
    else
    {
        $TagsAsString = "NULL"
    } 
    
    
    
}
}
}
  1. I am Trying to get all the subscription then
  2. I am Trying to get all the resource groups then
  3. I am Trying to get all the resource present in resource group
  4. Trying to get all the tags

But i am unable to get Tags and can anyone guide me how to export all tags into csv file.



Solution 1:[1]

As there is no $tag.Name and $Tag.Value , Your code doesn't store any values. The output you are looking are stored as $tags.keys and $tags.values. So, to collect them you will have to use for loop again . I tested the same by replacing the for each loop for the resources with the below script:

$TagsAsString=@() 
$tagkeys =@() 
$TagValues =@() 
 $resources = Get-AzResource -ResourceGroupName <resourcegroupname>
 foreach($resource in $resources){
       Write-host ("ResourceName :")$resource.Name
       if($resource.Tags -ne $null){ 
          foreach($Tag in $resource.Tags){
             foreach($key in $Tag.keys){
               $tagkeys += $key 
             }
             foreach($value in $Tag.values){
               $TagValues += $value
             }   
          }
       }
       else{
          Write-Host ("No Tags")
       }
}
for($i = 0; $i -lt $tagkeys.Length; $i++) {
    $TagsAsString=( '{0} : {1}' -f $tagkeys[$i], $tagvalues[$i] )
    Write-Host $TagsAsString 
}

Output:

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 AnsumanBal-MT