Quickstart: Create a Resource Graph shared query using Azure PowerShell

In this quickstart, you create an Azure Resource Graph shared query using the Az.ResourceGraph Azure PowerShell module. The module is included with the latest version of Azure PowerShell and adds cmdlets for Resource Graph.

A shared query can be run from Azure CLI with the experimental feature's commands, or you can run the shared query from the Azure portal. A shared query is an Azure Resource Manager object that you can grant permission to or run in Azure Resource Graph Explorer. When you finish, you can remove the Resource Graph extension.

Prerequisites

Install the module

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.

  1. Verify your PowerShellGet version:

    Get-Module -Name PowerShellGet
    

    If you need to update, go to PowerShellGet.

  2. Install the module:

    Install-Module -Name Az.ResourceGraph -Repository PSGallery -Scope CurrentUser
    

    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.

  3. Verify the module was installed:

    Get-Command -Module Az.ResourceGraph -CommandType Cmdlet
    

    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.

Connect-AzAccount

# Run these commands if you have multiple subscriptions
Get-AzSubScription
Set-AzContext -Subscription <subscriptionID>

Create a shared query

The shared query is an Azure Resource Manager object that you can grant permission to or run in Azure Resource Graph Explorer. The query summarizes the count of all resources grouped by location.

  1. Create a resource group to store the Azure Resource Graph shared query.

    New-AzResourceGroup -Name demoSharedQuery -Location westus2
    
  2. Create the Azure Resource Graph shared query.

    $params = @{
      Name = 'Summarize resources by location'
      ResourceGroupName = 'demoSharedQuery'
      Location = 'westus2'
      Description = 'This shared query summarizes resources by location for a pinnable map graphic.'
      Query = 'Resources | summarize count() by location'
    }
    
    New-AzResourceGraphQuery @params
    

    The $params variable uses PowerShell splatting to improve readability for the parameter values used in the command to create the shared query.

  3. List all shared queries in the resource group.

    Get-AzResourceGraphQuery -ResourceGroupName demoSharedQuery
    
  4. Limit the results to a specific shared query.

    Get-AzResourceGraphQuery -ResourceGroupName demoSharedQuery -Name 'Summarize resources by location'
    

Run the shared query

You can verify the shared query works using Azure Resource Graph Explorer. To change the scope, use the Scope menu on the left side of the page.

  1. Sign in to Azure portal.
  2. Enter resource graph into the search field at the top of the page.
  3. Select Resource Graph Explorer.
  4. Select Open query.
  5. Change Type to Shared queries.
  6. Select the query Count VMs by OS.
  7. Select Run query and the view output in the Results tab.
  8. Select Charts and then select Map to view the location map.

You can also run the query from your resource group.

  1. In Azure, go to the resource group, demoSharedQuery.
  2. From the Overview tab, select the query Count VMs by OS.
  3. Select the Results tab to view a list.
  4. Select Charts and then select Map to view the location map.

Clean up resources

When you finish, you can remove the Resource Graph shared query and resource group from your Azure environment. When a resource group is deleted, the resource group and all its resources are deleted.

Remove the shared query:

Remove-AzResourceGraphQuery -ResourceGroupName demoSharedQuery -Name 'Summarize resources by location'

Delete the resource group:

Remove-AzResourceGroup -Name demoSharedQuery

To sign out of your Azure PowerShell session:

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:

Remove-Module -Name Az.ResourceGraph

To uninstall the Az.ResourceGraph module from your computer, run the following command:

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 created a Resource Graph shared query using Azure PowerShell. To learn more about the Resource Graph language, continue to the query language details page.