Quickstart: Run Resource Graph query using Azure PowerShell
Article
This quickstart describes how to run an Azure Resource Graph query using the Az.ResourceGraph module for Azure PowerShell. The module is included with the latest version of Azure PowerShell and adds cmdlets for Resource Graph.
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.
Prerequisites
If you don't have an Azure account, create a free account before you begin.
If you installed the latest versions of PowerShell and Azure PowerShell, you already have the Az.ResourceGraph module and required version of PowerShellGet.
Optional module installation
Use the following steps to install the Az.ResourceGraph module so that you can use Azure PowerShell to run Azure Resource Graph queries. The Azure Resource Graph module requires PowerShellGet version 2.0.1 or higher.
The command installs the module in the CurrentUser scope. If you need to install in the AllUsers scope, run the installation from an administrative PowerShell session.
The command displays the Search-AzGraph cmdlet version and loads the module into your PowerShell session.
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.
Azure PowerShell
Connect-AzAccount# Run these commands if you have multiple subscriptionsGet-AzSubScriptionSet-AzContext -Subscription <subscriptionID>
Run a query
After the module 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 -ManagementGroup or -Subscription parameters.
Run an Azure Resource Graph query using the Search-AzGraph cmdlet:
Azure PowerShell
Search-AzGraph -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 the name property:
Azure PowerShell
Search-AzGraph -Query'Resources | project name, type | limit 5 | order by name asc'
Like the previous query, if you run this query multiple times 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.
Update the query to order by the name property and then limit the output to five results:
Azure PowerShell
Search-AzGraph -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.
If a query doesn't return results from a subscription you already have access to, then note that Search-AzGraph cmdlet defaults to subscriptions in the default context. To see the list of subscription IDs that are part of the default context, run this (Get-AzContext).Account.ExtendedProperties.Subscriptions If you wish to search across all the subscriptions you have access to, set the PSDefaultParameterValues for Search-AzGraph cmdlet by running $PSDefaultParameterValues=@{"Search-AzGraph:Subscription"= $(Get-AzSubscription).ID}
Clean up resources
To sign out of your Azure PowerShell session:
Azure PowerShell
Disconnect-AzAccount
Optional clean up steps
If you installed the latest version of Azure PowerShell, the Az.ResourceGraph module is included and shouldn't be removed. The following steps are optional if you did a manual install of the Az.ResourceGraph module and want to remove the module.
To remove the Az.ResourceGraph module from your PowerShell session, run the following command:
Azure PowerShell
Remove-Module -Name Az.ResourceGraph
To uninstall the Az.ResourceGraph module from your computer, run the following command:
Azure PowerShell
Uninstall-Module -Name Az.ResourceGraph
A message might be displayed that module Az.ResourceGraph is currently in use. If so, you need to shut down your PowerShell session and start a new session. Then run the command to uninstall the module from your computer.
Next steps
In this quickstart, you added the Resource Graph module to your Azure PowerShell environment and ran a query. To learn more, go to the query language details page.
Use Azure Resource Graph to run some advanced queries, including working with columns, listing tags used, and matching resources with regular expressions.