Quickstart: Run Resource Graph query using Azure CLI
This quickstart describes how to run an Azure Resource Graph query using the Azure CLI and the Resource Graph extension. The article also shows how to order (sort) and limit the query's results. You can run a query for resources in your tenant, management groups, or subscriptions. When you finish, you can remove the extension.
Prerequisites
- If you don't have an Azure account, create a free account before you begin.
- Azure CLI must be version 2.22.0 or higher for the Resource Graph extension.
- A Bash shell environment where you can run Azure CLI commands. For example, Git Bash in a Visual Studio Code terminal session.
Install the extension
To enable Azure CLI to query resources using Azure Resource Graph, the Resource Graph extension must be installed. The first time you run a query with az graph
a prompt is displayed to install the extension. Otherwise, use the following steps to do a manual installation.
List the available extensions and versions:
az extension list-available --output table
Install the extension:
az extension add --name resource-graph
Verify the extension was installed:
az extension list --output table
Display the extension's syntax:
az graph query --help
For more information about Azure CLI extensions, go to Use and manage extensions with the Azure CLI.
Connect to Azure
From a Visual Studio Code terminal session, connect to Azure. If you have more than one subscription, run the commands to set context to your subscription. Replace <subscriptionID>
with your Azure subscription ID.
az login
# Run these commands if you have multiple subscriptions
az account list --output table
az account set --subscription <subscriptionID>
Run a query
After the Azure CLI extension is added to your environment, you can run a tenant-based query. The query in this example returns five Azure resources with the name
and type
of each resource. To query by management group or subscription, use the --management-groups
or --subscriptions
arguments.
Run an Azure Resource Graph query:
az graph query --graph-query 'Resources | project name, type | limit 5'
This query example doesn't use a sort modifier like
order by
. If you run the query multiple times, it might yield a different set of resources for each request.Update the query to
order by
thename
property:az graph query --graph-query 'Resources | project name, type | limit 5 | order by name asc'
Like the previous query, if you run this query multiple times it might yield a different set of resources for each request. The order of the query commands is important. In this example, the
order by
comes after thelimit
. The query limits the results to five resources and then orders those results by name.Update the query to
order by
thename
property and thenlimit
the output to five results:az graph query --graph-query 'Resources | project name, type | order by name asc | limit 5'
If this query is run several times with no changes to your environment, the results are consistent and ordered by the
name
property, but still limited to five results. The query orders the results by name and then limits the output to five resources.
Clean up resources
To remove the Resource Graph extension, run the following command:
az extension remove --name resource-graph
To sign out of your Azure CLI session:
az logout
Next steps
In this quickstart, you ran Azure Resource Graph queries using the extension for Azure CLI. To learn more about the Resource Graph language, continue to the query language details page.