Find Azure Resources By Tags Using PowerShell


Recently, I have been requested to write a small PowerShell script to fetch all the Azure resources with no tags at all, but I thought of writing a comprehensive script that is not only to find all the resources with no tags but also to find resources with specific tag name(s), tag value(s), tag(s), or with all tags.

Tagging in Azure

I have already covered Tagging Microsoft Azure Resources Using Powershell (Az)  in my previous post, but just to brief…

Tags in Azure play pivotal role in managing the resources, predominantly in the cost governance strategies and much useful for automation and maintain environment hygiene. More than a resource name, tagging is very crucial and it must be consistent and appropriate across the resources in all the resource groups and subscriptions. Many organizations leverage the tagging effectively and consistently using the Azure policies or some automation techniques.

Find-AzResource

However, finding the resources in Azure is also crucial, and especially finding all the resources of all types from multiple subscriptions or resource groups. So I have come up with a PowerShell script to find all the Azure tagged/not tagged resources, and you can find the script in my GitHub repo…

 View the code in GitHub  

The script comes with an in-build help, and if you run the script without any parameters it will display the help as below…

C:\Users\kiran\PSScripts> . .\Find-AzResource.ps1
C:\Users\kiran\PSScripts> Find-AzResource

Output:

NAME
    Find-AzResource

SYNOPSIS
    Find-AzResource gets all the Azure tagged/not tagged resources,


SYNTAX
    Find-AzResource [-ResourceName <String[]>] [-Location <String[]>] [-ResourceType <String[]>] [<CommonParameters>]

    Find-AzResource -ResourceGroupName <String[]> [-ResourceName <String[]>] [-Location <String[]>] [-ResourceType <String[]>] -Tag <Hashtable> [<CommonParameters>]

    Find-AzResource -ResourceGroupName <String[]> [-ResourceName <String[]>] [-Location <String[]>] [-ResourceType <String[]>] -TagValue <String[]> [<CommonParameters>]

    Find-AzResource -ResourceGroupName <String[]> [-ResourceName <String[]>] [-Location <String[]>] [-ResourceType <String[]>] -TagName <String[]> [<CommonParameters>]

    Find-AzResource -ResourceGroupName <String[]> [-ResourceName <String[]>] [-Location <String[]>] [-ResourceType <String[]>] -WithNoTag [<CommonParameters>]

    Find-AzResource -ResourceGroupName <String[]> [-ResourceName <String[]>] [-Location <String[]>] [-ResourceType <String[]>] -AllTagged [<CommonParameters>]

    Find-AzResource -SubscriptionName <String[]> [-ResourceName <String[]>] [-Location <String[]>] [-ResourceType <String[]>] -Tag <Hashtable> [<CommonParameters>]

    Find-AzResource -SubscriptionName <String[]> [-ResourceName <String[]>] [-Location <String[]>] [-ResourceType <String[]>] -TagValue <String[]> [<CommonParameters>]

    Find-AzResource -SubscriptionName <String[]> [-ResourceName <String[]>] [-Location <String[]>] [-ResourceType <String[]>] -TagName <String[]> [<CommonParameters>]

    Find-AzResource -SubscriptionName <String[]> [-ResourceName <String[]>] [-Location <String[]>] [-ResourceType <String[]>] -WithNoTag [<CommonParameters>]

    Find-AzResource -SubscriptionName <String[]> [-ResourceName <String[]>] [-Location <String[]>] [-ResourceType <String[]>] -AllTagged [<CommonParameters>]


DESCRIPTION
    Find-AzResource gets all the Azure resources with...
    > All tags
    > No tags
    > Specific tag name(s)
    > Specific tag value(s)
    > Specific tag(s)
    ... from one or more resourcegroup(s) or subscripttion(s) and optionally filter the resources by location, name and type as well.

... output truncated ...

-------------------------- EXAMPLE 1 --------------------------

    PS > Find-AzResource

    Displays full help




    -------------------------- EXAMPLE 2 --------------------------

    PS > Find-AzResource -SubscriptionName Sub1, Sub2 -AllTagged

    Finds all the resources with tags in the given Subscriptions. it even works with ResourceGroupName as well.
    Optionally, you can even filter the resources by Name, Location and Type.




    -------------------------- EXAMPLE 3 --------------------------

    PS > Find-AzResource -SubscriptionName Sub1, Sub2 -WithNoTag

    Finds all the resources with no tags in the given Subscriptions. It even works with ResourceGroupName as well.
    Optionally, you can even filter the resources by Name, Location and Type.




    -------------------------- EXAMPLE 4 --------------------------

    PS > Find-AzResource -ResourceGroupName RG1, RG2 -TagName Status

    Finds all the resources with given tag name in the given resource groups. It even works with the subscriptions as well.
    Optionally, you can even filter the resources by Name, Location and Type.




    -------------------------- EXAMPLE 5 --------------------------

    PS > Find-AzResource -ResourceGroupName RG1, RG2 -TagValue HR, Finance

    Finds all the resources with given tag values in the given resource groups. It even works with the subscriptions as well.
    Optionally, you can even filter the resources by Name, Location and Type.




    -------------------------- EXAMPLE 6 --------------------------

    PS > Find-AzResource -ResourceGroupName RG1, RG2 -Tag @{Dept='IT'; Status="Expired"}

    Finds all the resources with given tags in the given resource groups. It even works with the subscriptions as well.
    Optionally, you can even filter the resources by Name, Location and Type.
 View the code in GitHub  
Share it on     |   |   |   | 
  Prev:  

Self Aliased Functions in PowerShell

  :Next  

Copy Security Rules From One Azure Network Security Group To Another Using PowerShell

comments powered by Disqus