Azure Resource Graph

Azure Resource Graph is an Azure service designed to extend Azure Resource Management by providing efficient and performant resource exploration with the ability to query at scale across a given set of subscriptions so that you can effectively govern your environment.

Resource types

Example queries

All resources with a CreatedOnDate tag

Azure Resource Graph Explorer

Resources
| where todatetime(tags.CreatedOnDate) <now(-30d)

Azure CLI

az graph query -q "Resources | where todatetime(tags.CreatedOnDate) <now(-30d) | project name"

Azure PowerShell

search-azgraph -query 'where todatetime(tags.CreatedOnDate) < now(-30d) | project name'

All resource groups with a CreatedOnDate tag

Azure Resource Graph Explorer

ResourceContainers
| where type =~ "microsoft.resources/subscriptions/resourcegroups"
| where todatetime(tags.CreatedOnDate) <now(-30d)

Azure CLI

az graph query -q "ResourceContainers | where type=='microsoft.resources/subscriptions/resourcegroups' | where todatetime(tags.CreatedOnDate) <now(-30d) | project name"

Azure PowerShell

az graph query -q "ResourceContainers | where type=='microsoft.resources/subscriptions/resourcegroups' | where todatetime(tags.CreatedOnDate) <now(-30d) | project name"

Resources with specific tag value

Azure PowerShell

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

All VMs ordered by name

Azure PowerShell

Search-AzGraph -Query "Resources | project name, location, type| where type =~ 'Microsoft.Compute/virtualMachines' | order by name desc"

Install Azure Resource Graph module locally

To enable Azure PowerShell to query Azure Resource Graph, the module must be added. This module can be used with locally installed PowerShell.

Requirements

Install the module

  • Open an administrative terminal and paste the following to install the module then check it’s installed
Install-Module -Name Az.ResourceGraph
  • Validate that the commands are available
Get-Command -Module 'Az.ResourceGraph' -CommandType 'Cmdlet'

References

https://docs.microsoft.com/en-us/azure/governance/resource-graph/

https://docs.microsoft.com/en-us/azure/governance/resource-graph/first-query-powershell?WT.mc_id=cloud5mins-youtube-frbouche#add-the-resource-graph-module

https://techcommunity.microsoft.com/t5/itops-talk-blog/azure-resource-graph-zero-to-hero/ba-p/2303572

https://docs.microsoft.com/en-us/answers/questions/39241/azure-resource-graph-query-for-empty-resource-grou.html

Last modified July 21, 2024: update (e2ae86c)