Quickstart: Create a Resource Graph shared query using Azure PowerShell

This article describes how you can create an Azure Resource Graph shared query using the Az.ResourceGraph PowerShell module.

Prerequisites

  • If you don't have an Azure subscription, create a free account before you begin.

Important

While the Az.ResourceGraph PowerShell module is in preview, you must install it separately using the Install-Module cmdlet.

Install-Module -Name Az.ResourceGraph -Scope CurrentUser -Repository PSGallery -Force
  • If you have multiple Azure subscriptions, choose the appropriate subscription in which the resources should be billed. Select a specific subscription using the Set-AzContext cmdlet.

    Set-AzContext -SubscriptionId 00000000-0000-0000-0000-000000000000
    

Create a Resource Graph shared query

With the Az.ResourceGraph PowerShell module added to your environment of choice, it's time to create a Resource Graph 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 with New-AzResourceGroup to store the Azure Resource Graph shared query. This resource group is named resource-graph-queries and the location is westus2.

    # Login first with `Connect-AzAccount` if not using Cloud Shell
    
    # Create the resource group
    New-AzResourceGroup -Name resource-graph-queries -Location westus2
    
  2. Create the Azure Resource Graph shared query using the Az.ResourceGraph PowerShell module and New-AzResourceGraphQuery cmdlet:

    # Create the Azure Resource Graph shared query
    $Params = @{
      Name = 'Summarize resources by location'
      ResourceGroupName = 'resource-graph-queries'
      Location = 'westus2'
      Description = 'This shared query summarizes resources by location for a pinnable map graphic.'
      Query = 'Resources | summarize count() by location'
    }
    New-AzResourceGraphQuery @Params
    
  3. List the shared queries in the new resource group. The Get-AzResourceGraphQuery cmdlet returns an array of values.

    # List all the Azure Resource Graph shared queries in a resource group
    Get-AzResourceGraphQuery -ResourceGroupName resource-graph-queries
    
  4. To get just a single shared query result, use Get-AzResourceGraphQuery with its Name parameter.

    # Show a specific Azure Resource Graph shared query
    Get-AzResourceGraphQuery -ResourceGroupName resource-graph-queries -Name 'Summarize resources by location'
    

Clean up resources

If you wish to remove the Resource Graph shared query and resource group from your Azure environment, you can do so by using the following commands:

# Delete the Azure Resource Graph shared query
Remove-AzResourceGraphQuery -ResourceGroupName resource-graph-queries -Name 'Summarize resources by location'

# Remove the resource group
# WARNING: This command deletes ALL resources you've added to this resource group
Remove-AzResourceGroup -Name resource-graph-queries

Next steps

In this quickstart, you've created a Resource Graph shared query using Azure PowerShell. To learn more about the Resource Graph language, continue to the query language details page.