Search code examples
azuretagsazure-powershellazure-automation

How to identify untagged resources forr tag name and value using azure powershell


I am looking for a PowerShell script for getting the list of resources not having specific tagname/value in Azure, am having the cmdlet for this as I mentioned below, but don’t know how to create a complete script using functions, parameters etc.

Please help.

Many thanks,

$Tags = @{"environment"="Terraform Demo"}

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

Please help me with this logic correct me if this not required. Thanks in advance.


Solution

  • If you want to receive resources that do not have any tags configured at all, you could use this query:

        Get-AzResource | Where-Object {$_.Tags -eq $null}
    

    If you want to receive resources that have the environment tag set to $nulll, you could use this query:

        Get-AzResource | Where-Object {$_.Tags.environment -eq ''}
    

    Contrarily, if you want to find resources that do not have the tag combination "environment" = "Terraform Demo" you could use:

        Get-AzResource | Where-Object {$_.Tags.environment -ne 'Terraform Demo'}
    

    You might also want to check switching to Resource Graph queries, since you would not have to change the context when searching across multiple subscriptions. Checking for resources that do not have an environment tag configured would work like this:

    Search-AzGraph -Query "Resources | where tags.environment=~'' | project name"
    

    The latter requires the Az.ResourceGraph module. See here for details: https://learn.microsoft.com/en-us/azure/governance/resource-graph/samples/starter?tabs=azure-powershell#list-tag