|
1 | 1 | ---
|
2 |
| -title: 'Quickstart: Your first PowerShell query' |
3 |
| -description: In this quickstart, you follow the steps to enable the Resource Graph module for Azure PowerShell and run your first query. |
4 |
| -ms.date: 06/15/2022 |
| 2 | +title: "Quickstart: Run Resource Graph query using Azure PowerShell" |
| 3 | +description: In this quickstart, you run an Azure Resource Graph query using the module for Azure PowerShell. |
| 4 | +ms.date: 04/24/2024 |
5 | 5 | ms.topic: quickstart
|
6 | 6 | ms.custom: mode-api, devx-track-azurepowershell
|
7 | 7 | ---
|
8 |
| -# Quickstart: Run your first Resource Graph query using Azure PowerShell |
9 | 8 |
|
10 |
| -The first step to using Azure Resource Graph is to check that the module for Azure PowerShell is |
11 |
| -installed. This quickstart walks you through the process of adding the module to your Azure |
12 |
| -PowerShell installation. |
| 9 | +# Quickstart: Run Resource Graph query using Azure PowerShell |
13 | 10 |
|
14 |
| -At the end of this process, you'll have added the module to your Azure PowerShell installation of |
15 |
| -choice and run your first Resource Graph query. |
| 11 | +This quickstart describes how to run an Azure Resource Graph query using the `Az.ResourceGraph` module for Azure PowerShell. 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 module. |
16 | 12 |
|
17 | 13 | ## Prerequisites
|
18 | 14 |
|
19 |
| -If you don't have an Azure subscription, create a [free](https://azure.microsoft.com/free/) account |
20 |
| -before you begin. |
| 15 | +- If you don't have an Azure account, create a [free account](https://azure.microsoft.com/free/?WT.mc_id=A261C142F) before you begin. |
| 16 | +- [PowerShell](/powershell/scripting/install/installing-powershell). |
| 17 | +- [Azure PowerShell](/powershell/azure/install-azure-powershell). |
| 18 | +- [Visual Studio Code](https://code.visualstudio.com/). |
21 | 19 |
|
22 |
| -[!INCLUDE [cloud-shell-try-it.md](../../../includes/cloud-shell-try-it.md)] |
| 20 | +## Install the module |
23 | 21 |
|
24 |
| -## Add the Resource Graph module |
| 22 | +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. If you installed the latest versions of PowerShell and Azure PowerShell, you already have the required version. |
25 | 23 |
|
26 |
| -To enable Azure PowerShell to query Azure Resource Graph, the module must be added. This module can |
27 |
| -be used with locally installed PowerShell, with [Azure Cloud Shell](https://shell.azure.com), or |
28 |
| -with the [PowerShell Docker image](https://hub.docker.com/_/microsoft-powershell). |
| 24 | +1. Verify your PowerShellGet version: |
29 | 25 |
|
30 |
| -### Base requirements |
| 26 | + ```azurepowershell |
| 27 | + Get-Module -Name PowerShellGet |
| 28 | + ``` |
31 | 29 |
|
32 |
| -The Azure Resource Graph module requires the following software: |
| 30 | + If you need to update, go to [PowerShellGet](/powershell/gallery/powershellget/install-powershellget). |
33 | 31 |
|
34 |
| -- Azure PowerShell 1.0.0 or higher. If it isn't yet installed, follow |
35 |
| - [these instructions](/powershell/azure/install-azure-powershell). |
| 32 | +1. Install the module: |
36 | 33 |
|
37 |
| -- PowerShellGet 2.0.1 or higher. If it isn't installed or updated, follow |
38 |
| - [these instructions](/powershell/gallery/powershellget/install-powershellget). |
39 |
| - |
40 |
| -### Install the module |
| 34 | + ```azurepowershell |
| 35 | + Install-Module -Name Az.ResourceGraph -Repository PSGallery -Scope CurrentUser |
| 36 | + ``` |
41 | 37 |
|
42 |
| -The Resource Graph module for PowerShell is **Az.ResourceGraph**. |
| 38 | + 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. |
43 | 39 |
|
44 |
| -1. From an **administrative** PowerShell prompt, run the following command: |
| 40 | +1. Verify the module was installed: |
45 | 41 |
|
46 |
| - ```azurepowershell-interactive |
47 |
| - # Install the Resource Graph module from PowerShell Gallery |
48 |
| - Install-Module -Name Az.ResourceGraph |
| 42 | + ```azurepowershell |
| 43 | + Get-Command -Module Az.ResourceGraph -CommandType Cmdlet |
49 | 44 | ```
|
50 | 45 |
|
51 |
| -1. Validate that the module has been imported and is at least version `0.11.0`: |
| 46 | + The command displays the `Search-AzGraph` cmdlet version and loads the module into your PowerShell session. |
52 | 47 |
|
53 |
| - ```azurepowershell-interactive |
54 |
| - # Get a list of commands for the imported Az.ResourceGraph module |
55 |
| - Get-Command -Module 'Az.ResourceGraph' -CommandType 'Cmdlet' |
56 |
| - ``` |
| 48 | +## Connect to Azure |
57 | 49 |
|
58 |
| -## Run your first Resource Graph query |
| 50 | +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. |
| 51 | + |
| 52 | +```azurepowershell |
| 53 | +Connect-AzAccount |
| 54 | +
|
| 55 | +# Run these commands if you have multiple subscriptions |
| 56 | +Get-AzSubScription |
| 57 | +Set-AzContext -Subscription <subscriptionID> |
| 58 | +``` |
59 | 59 |
|
60 |
| -With the Azure PowerShell module added to your environment of choice, it's time to try out a simple |
61 |
| -tenant-based Resource Graph query. The query returns the first five Azure resources with the |
62 |
| -**Name** and **Resource Type** of each resource. To query by |
63 |
| -[management group](../management-groups/overview.md) or subscription, use the `-ManagementGroup` |
64 |
| -or `-Subscription` parameters. |
| 60 | +## Run a query |
65 | 61 |
|
66 |
| -1. Run your first Azure Resource Graph query using the `Search-AzGraph` cmdlet: |
| 62 | +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](../management-groups/overview.md) or subscription, use the `-ManagementGroup` or `-Subscription` parameters. |
67 | 63 |
|
68 |
| - ```azurepowershell-interactive |
69 |
| - # Login first with Connect-AzAccount if not using Cloud Shell |
| 64 | +1. Run an Azure Resource Graph query using the `Search-AzGraph` cmdlet: |
70 | 65 |
|
71 |
| - # Run Azure Resource Graph query |
| 66 | + ```azurepowershell |
72 | 67 | Search-AzGraph -Query 'Resources | project name, type | limit 5'
|
73 | 68 | ```
|
74 | 69 |
|
75 |
| - > [!NOTE] |
76 |
| - > As this query example doesn't provide a sort modifier such as `order by`, running this query |
77 |
| - > multiple times is likely to yield a different set of resources per request. |
| 70 | + 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. |
78 | 71 |
|
79 |
| -1. Update the query to `order by` the **Name** property: |
| 72 | +1. Update the query to `order by` the `name` property: |
80 | 73 |
|
81 |
| - ```azurepowershell-interactive |
82 |
| - # Run Azure Resource Graph query with 'order by' |
| 74 | + ```azurepowershell |
83 | 75 | Search-AzGraph -Query 'Resources | project name, type | limit 5 | order by name asc'
|
84 | 76 | ```
|
85 | 77 |
|
86 |
| - > [!NOTE] |
87 |
| - > Just as with the first query, running this query multiple times is likely to yield a different |
88 |
| - > set of resources per request. The order of the query commands is important. In this example, |
89 |
| - > the `order by` comes after the `limit`. This command order first limits the query results and |
90 |
| - > then orders them. |
| 78 | + 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. |
91 | 79 |
|
92 |
| -1. Update the query to first `order by` the **Name** property and then `limit` to the top five |
93 |
| - results: |
| 80 | +1. Update the query to `order by` the `name` property and then `limit` the output to five results: |
94 | 81 |
|
95 |
| - ```azurepowershell-interactive |
96 |
| - # Store the query in a variable |
97 |
| - $query = 'Resources | project name, type | order by name asc | limit 5' |
98 |
| -
|
99 |
| - # Run Azure Resource Graph query with `order by` first, then with `limit` |
100 |
| - Search-AzGraph -Query $query |
| 82 | + ```azurepowershell |
| 83 | + Search-AzGraph -Query 'Resources | project name, type | order by name asc | limit 5' |
101 | 84 | ```
|
102 | 85 |
|
103 |
| -When the final query is run several times, assuming that nothing in your environment changes, |
104 |
| -the results returned are consistent and ordered by the **Name** property, but still limited to the |
105 |
| -top five results. |
| 86 | + 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. |
106 | 87 |
|
107 |
| -> [!NOTE] |
108 |
| -> If the query does not return results from a subscription you already have access to, then note |
109 |
| -> that `Search-AzGraph` cmdlet defaults to subscriptions in the default context. To see the list of |
110 |
| -> subscription IDs which are part of the default context run this |
111 |
| -> `(Get-AzContext).Account.ExtendedProperties.Subscriptions` If you wish to search across all the |
112 |
| -> subscriptions you have access to, one can set the PSDefaultParameterValues for `Search-AzGraph` |
113 |
| -> cmdlet by running |
114 |
| -> `$PSDefaultParameterValues=@{"Search-AzGraph:Subscription"= $(Get-AzSubscription).ID}` |
| 88 | +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}` |
115 | 89 |
|
116 | 90 | ## Clean up resources
|
117 | 91 |
|
118 |
| -If you wish to remove the Resource Graph module from your Azure PowerShell environment, you can do |
119 |
| -so by using the following command: |
| 92 | +To remove the `Az.ResourceGraph` module from your PowerShell session, run the following command: |
| 93 | + |
| 94 | +```azurepowershell |
| 95 | +Remove-Module -Name Az.ResourceGraph |
| 96 | +``` |
120 | 97 |
|
121 |
| -```azurepowershell-interactive |
122 |
| -# Remove the Resource Graph module from the current session |
123 |
| -Remove-Module -Name 'Az.ResourceGraph' |
| 98 | +To uninstall the `Az.ResourceGraph` module from your computer, run the following command: |
124 | 99 |
|
125 |
| -# Uninstall the Resource Graph module from the environment |
126 |
| -Uninstall-Module -Name 'Az.ResourceGraph' |
| 100 | +```azurepowershell |
| 101 | +Uninstall-Module -Name Az.ResourceGraph |
127 | 102 | ```
|
128 | 103 |
|
129 |
| -> [!NOTE] |
130 |
| -> This doesn't delete the module file downloaded earlier. It only removes it from the running |
131 |
| -> PowerShell session. |
| 104 | +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. |
| 105 | + |
| 106 | +To sign out of your Azure PowerShell session: |
| 107 | + |
| 108 | +```azurepowershell |
| 109 | +Disconnect-AzAccount |
| 110 | +``` |
132 | 111 |
|
133 | 112 | ## Next steps
|
134 | 113 |
|
135 |
| -In this quickstart, you've added the Resource Graph module to your Azure PowerShell environment and |
136 |
| -run your first query. To learn more about the Resource Graph language, continue to the query |
137 |
| -language details page. |
| 114 | +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. |
138 | 115 |
|
139 | 116 | > [!div class="nextstepaction"]
|
140 |
| -> [Get more information about the query language](./concepts/query-language.md) |
| 117 | +> [Understanding the Azure Resource Graph query language](./concepts/query-language.md) |
0 commit comments