Skip to main content
Jorge Bernhardt Jorge Bernhardt
  1. Posts/

Run Azure Resource Graph queries using Azure PowerShell

·498 words·3 mins· 100 views · 5 likes ·
Azure PowerShell Azure Resource Graph Connect-AzAccount Get-AzSubscription

In my last article, I showed you how to enable the Azure Resource Graph module in PowerShell and Azure CLI. Today I want to show you how to use this service to make queries and explore the resources using Azure PowerShell. Requirements:

  • This tutorial assumes that you already have a Microsoft Azure account set up.
  • Have the Azure Resource Graph module enabled in Azure PowerShell, check out this link.
Azure PowerShell Workaround>

Azure PowerShell Workaround #

If you want to know how to install the PowerShell Azure module on your machine, check out this link. The simplest way to get started is to sign in interactively at the command line.

Connect-AzAccount

This cmdlet will bring up a dialog box prompting you for your email address and password associated with your Azure account. If you have more than one subscription associated with your mail account, you can choose the default subscription. To perform this task, we will use the following commands:

Get-AzSubscription
Select-AzSubscription -Subscription "My Subscription"

Once you set your default subscription, you’re ready to start.

Using the Search-AzGraph cmdlet.>

Using the Search-AzGraph cmdlet. #

To Run an Azure Resource Graph query, you should use the Search-AzGraph cmdlet with the following syntax. The following query returns the number of Azure resources that exist in the subscriptions to which you have access.

Search-AzGraph `
    -Query 'summarize count()'

Search-AzGraph -Query
We can modify the previous query to obtain the number of resources by type of resources.

Search-AzGraph `
    -Query 'summarize count() by type'

Search-AzGraph -Query
We can also list all the resources of our subscription, using the following query.

Search-AzGraph `
    -Query 'project name, type'

Search-AzGraph -Query
Now that we know the types of resources that exist in our subscription, I will use one of them to make the queries, in my case I will use virtual machines as an example of this guide. This query returns the name and resource group properties of type resources: microsoft.compute/virtualmachines

Search-AzGraph `
    -Query "where type =~ 'microsoft.compute/virtualmachines' `
    | project name, resourceGroup"

Search-AzGraph -Query “where type”
Here is another variant of the same type of query

Search-AzGraph `
    -Query "where type =~ 'microsoft.compute/virtualmachines' `
    | project name, location, tags"

Azure Resource Graph PowerShell
We can also add tabular operators to our queries, check out this link. In this case, I will use the count operator.

Search-AzGraph `
    -Query "where type =~ 'microsoft.compute/virtualmachines' `
    | project name | count"

Search-AzGraph -Query “where type”
In the following query, I will use the summarize operator.

Search-AzGraph `
    -Query "where type =~ 'microsoft.compute/virtualmachines' `
    | project name, location `
    | summarize count() by location"

Azure Resource Graph PowerShell
This query searches for virtual machines that match a regular expression (known as regex). Regex @ matches allow us to define the regular expression to match string or characters group.

Search-AzGraph `
    -Query "where type =~ 'microsoft.compute/virtualmachines' and name matches regex @'DEMO' `
    | project name, location"

Azure Resource Graph Regex
In the next post, I will show you how to run queries in your environment using Azure CLI. If you want to know more about the Azure Resource Graph query language, check out this link.