How to get all Azure Resources without tags in a Azure Resource Group

15.1k views Asked by At

In my Azure dev/test lab (DTL), there are many resources which were not tagged. How can I get a list of all untagged resources under DTL/resource group?

5

There are 5 answers

1
DevX On BEST ANSWER

This link has the solution for this question. It beautifully explains assigning and querying tags using powershell.

$resourceGroupName = 'InternalReportingRGDev'

$azureRGInfo = Get-AzureRmResourceGroup -Name $resourceGroupName foreach ($item in $azureRGInfo)

{

Find-AzureRmResource -ResourceGroupNameEquals $item.ResourceGroupName | ForEach-Object {Set-AzureRmResource -ResourceId $PSItem.ResourceId -Tag $item.Tags -Force }

}

0
huysmania On

Here's a simple PowerShell loop to get untagged resources.

$resources = Get-AzureRmResource
foreach($resource in $resources)
{
    if ($resource.Tags -eq $null)
    {
        echo $resource.Name, $resource.ResourceType
    }
}

Other ways to query this information and also set tags programmatically or as part of resource deployments are described here.

If you want to avoid the situation of ending up with untagged resources, you could enforce a customized policy that all resources should have a value for a particular tag.

1
Pankaj negi On

<#Bellow is PowerShell script to locate untagged resources - you may change the script out put as per your requirement. Hope must be helpful. Thanks!#>

Write-Host "List all resource where Tag value is not Set"
Write-Host "********************************************"
#Fetch all resource details
$resources=get-AzureRmResource
foreach ($resource in $resources) {
    $tagcount=(get-AzureRmResource | where-object {$_.Name -match $resource.Name}).Tags.count
    if($tagcount -eq 0) {
        Write-Host "Resource Name - "$resource.Name
        Write-Host "Resource Type and RG Name : " $resource.resourcetype " & " $resource.resourcegroupname "`n"
    }
 }
0
JohnC On

Here is the idiomatic PowerShell to supplement @huysmania's answer which is expressed in procedural language mindset (and updated for the new PowerShell Az cmdlets):

Get-AzResource | Where-Object Tags -eq $null | Select-Object -Property Name, ResourceType

and the terse (alias) form:

Get-AzResource | ? Tags -eq $null | select Name, ResourceType
0
RoadRunner On

I usually just run this command to output a table of untagged resources using Get-AzResource. It filters Azure resources with tags that are $null or empty using Where-Object.

Get-AzResource `
    | Where-Object {$null -eq $_.Tags -or $_.Tags.Count -eq 0} `
    | Format-Table -AutoSize

If you want to list untagged resources for a specific resource group, you can just add the -ResourceGroupName switch to Get-AzResource.

$resourceGroupName = "My Resource Group"

Get-AzResource -ResourceGroupName $resourceGroupName `
    | Where-Object {$null -eq $_.Tags -or $_.Tags.Count -eq 0} `
    | Format-Table -AutoSize

Note: The above uses the newer Azure PowerShell Az module, which is replacement for AzureRM.