↓ Skip to main content

Azure Resource Graph - A Practical Introduction

Take Bakker
Author
Take Bakker

A few years ago I discovered Azure Resource Graph, but for a long time I only used it to track down the occasional resource. That changed during the rebuild of our inventory script. It turns out to be a very good fit for automation and inventory scripts across subscriptions and tenants.

What is Azure Resource Graph
#

Azure Resource Graph (ARG) is a query service built into Azure that lets you explore your Azure resources at scale. You write queries in Kusto Query Language (KQL), which you’ll most likely already know from Log Analytics. These run against a constantly updated index of your resource metadata.

The key difference from the classic approach: ARG reads from an index, not from ARM directly. That makes it a lot faster than running Get-AzVM, Get-AzResource or similar PowerShell cmdlets across multiple subscriptions. In my own test across ten subscriptions it was roughly 47 times faster.

Advantages of ARG
#

We manage resources across multiple subscriptions and customer tenants. When using the native PowerShell cmdlets this means switching context for each subscription.

With ARG one query lists resources across every tenant you have access to, through Azure Lighthouse or guest access. If you only want one, filter on tenantId. You only see what your account has read access to, so the results follow your RBAC permissions.

The difference shows when you compare ARG to the classic PowerShell approach:

$subscriptions = Get-AzSubscription

$results = foreach ($sub in $subscriptions) {
  Set-AzContext -SubscriptionId $sub.Id | Out-Null
  Get-AzVM
  Get-AzSqlInstance
}

That’s a context switch and two ARM calls per subscription. For ten subscriptions, that’s thirty sequential operations before you have a complete picture. Get-AzVM is the quick one, too: Get-AzSqlInstance takes even longer.

The ARG equivalent:

resources
| where type in~ (
    "microsoft.compute/virtualmachines",
    "microsoft.sql/managedinstances"
  )
| project name, type, resourceGroup, location, subscriptionId

One query, one call. I measured ARG against two classic approaches on the same ten subscriptions: the loop above, and a Get-AzResource call per subscription for the same resource types. There are 270 virtual machines and SQL managed instances in total, and all three returned the same 270 rows. This is the median of several runs:

ApproachMedianFastestSlowest
ARG, one query0.33 s0.23 s0.45 s
Classic loop (Get-AzVM + Get-AzSqlInstance)15.62 s15.56 s15.97 s
Get-AzResource per subscription13.27 s12.70 s13.83 s

The classic loop needed about 15 seconds where ARG needed a third of a second (measured in November 2026). The index is refreshed frequently enough that it’s accurate for most practical purposes.

This is also what changed in our inventory script. Before, it had to loop over subscriptions and call the cmdlets for each resource type. Now it is a set of straightforward queries, which makes it much quicker to run and a lot simpler to read and maintain.

Querying ARG
#

Queries are written in KQL. The main table you’ll most likely start to work with is Resources. From there you can apply filters, project columns and join across tables. It is worth exploring what other tables are available too, like PolicyResources.

resources
| where type =~ "microsoft.compute/virtualmachines"
| project name, resourceGroup, location, subscriptionId
| order by name asc
Note

You’ll notice =~ in the where clause instead of ==. In KQL == is a case-sensitive comparison, while =~ ignores case. A single = is not a comparison operator at all, it is only used for assignment, like subscriptionName = name in a project.

In practice I use =~ for resource types because the casing you see in ARM, in templates and in ARG results doesn’t always match. Microsoft.Compute/virtualMachines and microsoft.compute/virtualmachines are the same type, and a case-sensitive filter will quietly return nothing if you get it wrong. The same goes for in~ when you match against a list, and !~ when you want to exclude.

Portal
#

The Azure Resource Graph Explorer in the portal is useful for building and testing queries before you put them into scripts. You get instant feedback, and the table list on the left shows what you can query.

It also lets you open a row and browse the full object, including properties and all nested objects. That makes it easy to find the exact property you need and add it to your query. You can do the same in PowerShell, but sometimes a UI just gets you there quicker.

Azure Resource Graph Explorer in the Azure portal, showing a query with its results

PowerShell
#

From PowerShell you use Search-AzGraph which is part of the Az.ResourceGraph module. For multi-line queries I advise using a here-string, because it keeps the query readable:

$query = @"
resources
| where type =~ "microsoft.compute/virtualmachines"
| project id, name, resourceGroup, location, subscriptionId
| order by name asc
"@

Search-AzGraph -Query $query

You can scope it with -Subscription for a specific list, or -ManagementGroup to target an entire management group. Add -UseTenantScope to query across all subscriptions in the current tenant.

The other thing you need to know: ARG results are capped by default at 100 per call when using PowerShell or Azure CLI. If you’re inventorying a large environment you’ll hit that limit and silently miss resources. The fix is to raise the limit with -First 1000, which is the maximum. If you need more than that, loop on the skip token until it’s empty:

$results = [System.Collections.Generic.List[object]]::new()
$skipToken = $null

do {
  $response = Search-AzGraph -Query $query -First 1000 -SkipToken $skipToken
  $results.AddRange($response)
  $skipToken = $response.SkipToken
} while ($skipToken)
Warning

Without this, any environment with more than 1000 resources of a given type will give you an incomplete result with no warning. Not something you want to discover after handing over an inventory report.

Also note that id is in the projection of $query. The documentation says the skip token only works when id is projected. I could not make paging fail without it on Az.ResourceGraph 1.3.0, but it costs nothing to include.

Converting subscription IDs to names
#

ARG returns subscriptionId as a GUID in results. Which is fine for automation, but if you’re building a report for a client raw GUIDs don’t mean much. The fix is easy: a join against the ResourceContainers table. This table holds the subscription metadata.

resources
| join kind=leftouter (
    resourcecontainers
    | where type =~ "microsoft.resources/subscriptions"
    | project subscriptionId, subscriptionName = name
) on subscriptionId
| project name, resourceGroup, location, subscriptionName

ResourceContainers is worth knowing about in general. It covers subscriptions, resource groups, and management groups. The join is straightforward: match on subscriptionId, pull the name and drop the raw ID from the output.

Throttling
#

ARG is a free service and throttling is part of that. The default limit is 15 queries per 5-second window per user, though Microsoft states this value is subject to change. Hit the limit and you get a RateLimiting error.

Every response includes two headers you can use to track where you stand:

  • x-ms-user-quota-remaining: queries left in the current window
  • x-ms-user-quota-resets-after: time until quota resets, in hh:mm:ss format

I tried to hit the limit for this article with a burst of 40 rapid Search-AzGraph calls. No throttling error reached my script, but one call took 10 seconds, which suggests the cmdlet backs off and retries on its own. I couldn’t prove that, so don’t count on it. Personally I have never hit these limits, but Microsoft has official throttling guidance covering grouping and staggering queries in detail if you need to go further.

ARG orphaned resources
#

azure-orphan-resources is a GitHub repo containing ARG queries for orphaned resources in Azure, including a method to create workbooks to show them in the Azure Portal. It is a useful base management layer for your Azure Landing Zone. The repo hasn’t been updated in a while, but it is still a solid starting point and shows what ARG can do. We’ve expanded it with additional queries, for Firewall Policies for example.

A Firewall Policy is orphaned when no firewall uses it. The catch is that a policy without a linked firewall can still be in use, because it can be the base policy of a child policy that is linked. Check both:

resources
| where type =~ "microsoft.network/firewallpolicies"
| extend firewallCount = array_length(properties.firewalls)
| extend childPolicyCount = array_length(properties.childPolicies)
| where firewallCount == 0 and childPolicyCount == 0
| project name, resourceGroup, location, subscriptionId, tier = tostring(properties.sku.tier)

In my environment this returns nothing. Two policies have no firewall linked, but both are base policies with child policies, so a check on firewalls alone would have flagged them wrongly.

What ARG doesn’t cover
#

ARG doesn’t expose everything. Most of the time the gap shows up with child resources and nested details. Diagnostic settings are not in ARG at all, and VM extension details often aren’t fully available. Child resources can be missing as well: ARG holds my Firewall Policies, but not their rule collection groups, so for the rules themselves I call the ARM REST API per policy. The good news is more and more detail is becoming available so always keep your eyes open on what is actually available.

When the detail isn’t there, you fall back to direct ARM calls or PowerShell. In practice I use ARG to get most of the information and to identify what is missing, then pull the specifics with targeted cmdlets. ARG doesn’t replace those calls, it gives you a faster first pass.

Where to start
#

If you’re not using ARG yet and you work across multiple subscriptions, a lot of resources or in a managed services capacity, it’s worth adding to your toolkit. Setup is minimal: you need the Az.ResourceGraph module and read access on the Azure Subscription(s). It works across the tenants you already have access to and finishes in a fraction of the time the PowerShell cmdlets need.

Start in the Resource Graph Explorer. Build a few queries for the things you check most often. You’ll stop reaching for Get-AzResource faster than you expect.