Quickstart: Run Resource Graph query using Azure CLI

This quickstart describes how to run an Azure Resource Graph query using the extension for Azure CLI. 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're finished, you can remove the extension.

Prerequisites

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>

Install the extension

To enable Azure CLI to query resources using Azure Resource Graph, the Resource Graph extension must be installed. You can manually install the extension with the following steps. Otherwise, the first time you run a query with az graph you're prompted to install the extension.

  1. List the available extensions and versions:

    az extension list-available --output table
    
  2. Install the extension:

    az extension add --name resource-graph
    
  3. Verify the extension was installed:

    az extension list --output table
    
  4. 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.

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.

  1. 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.

  2. Update the query to order by the name 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 the limit. The query limits the results to five resources and then orders those results by name.

  3. Update the query to order by the name property and then limit 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, go to the query language details article.