Skip to content

Commit ff0819a

Browse files
Merge pull request #273164 from davidsmatlak/ds-update-arg-pwsh-quickstart-20240423
Updates ARG quickstart for Azure PowerShell
2 parents c4611e6 + 82fcf45 commit ff0819a

File tree

4 files changed

+68
-91
lines changed

4 files changed

+68
-91
lines changed

articles/governance/resource-graph/changes/get-resource-changes.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ In this article, you learn:
2222

2323
## Prerequisites
2424

25-
- To enable Azure PowerShell to query Azure Resource Graph, [add the module](../first-query-powershell.md#add-the-resource-graph-module).
25+
- To enable Azure PowerShell to query Azure Resource Graph, [add the module](../first-query-powershell.md#install-the-module).
2626
- To enable Azure CLI to query Azure Resource Graph, [add the extension](../first-query-azurecli.md#install-the-extension).
2727

2828
## Understand change event properties
Lines changed: 65 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,140 +1,117 @@
11
---
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
55
ms.topic: quickstart
66
ms.custom: mode-api, devx-track-azurepowershell
77
---
8-
# Quickstart: Run your first Resource Graph query using Azure PowerShell
98

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
1310

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.
1612

1713
## Prerequisites
1814

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/).
2119

22-
[!INCLUDE [cloud-shell-try-it.md](../../../includes/cloud-shell-try-it.md)]
20+
## Install the module
2321

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.
2523

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:
2925

30-
### Base requirements
26+
```azurepowershell
27+
Get-Module -Name PowerShellGet
28+
```
3129
32-
The Azure Resource Graph module requires the following software:
30+
If you need to update, go to [PowerShellGet](/powershell/gallery/powershellget/install-powershellget).
3331
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:
3633
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+
```
4137

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.
4339

44-
1. From an **administrative** PowerShell prompt, run the following command:
40+
1. Verify the module was installed:
4541

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
4944
```
5045

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.
5247

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
5749

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+
```
5959

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
6561

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.
6763

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:
7065

71-
# Run Azure Resource Graph query
66+
```azurepowershell
7267
Search-AzGraph -Query 'Resources | project name, type | limit 5'
7368
```
7469

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.
7871

79-
1. Update the query to `order by` the **Name** property:
72+
1. Update the query to `order by` the `name` property:
8073

81-
```azurepowershell-interactive
82-
# Run Azure Resource Graph query with 'order by'
74+
```azurepowershell
8375
Search-AzGraph -Query 'Resources | project name, type | limit 5 | order by name asc'
8476
```
8577

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.
9179

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:
9481

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'
10184
```
10285

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.
10687

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}`
11589

11690
## Clean up resources
11791

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+
```
12097

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:
12499

125-
# Uninstall the Resource Graph module from the environment
126-
Uninstall-Module -Name 'Az.ResourceGraph'
100+
```azurepowershell
101+
Uninstall-Module -Name Az.ResourceGraph
127102
```
128103

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+
```
132111

133112
## Next steps
134113

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.
138115

139116
> [!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)

articles/governance/resource-graph/samples/advanced.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ before you begin.
3838
Azure CLI (through an extension) and Azure PowerShell (through a module) support Azure Resource
3939
Graph. Before running any of the following queries, check that your environment is ready. See
4040
[Azure CLI](../first-query-azurecli.md#install-the-extension) and [Azure
41-
PowerShell](../first-query-powershell.md#add-the-resource-graph-module) for steps to install and
41+
PowerShell](../first-query-powershell.md#install-the-module) for steps to install and
4242
validate your shell environment of choice.
4343

4444
## Show resource types and API versions

articles/governance/resource-graph/samples/starter.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ before you begin.
4242
Azure CLI (through an extension) and Azure PowerShell (through a module) support Azure Resource
4343
Graph. Before running any of the following queries, check that your environment is ready. See
4444
[Azure CLI](../first-query-azurecli.md#install-the-extension) and [Azure
45-
PowerShell](../first-query-powershell.md#add-the-resource-graph-module) for steps to install and
45+
PowerShell](../first-query-powershell.md#install-the-module) for steps to install and
4646
validate your shell environment of choice.
4747

4848
## Count Azure resources

0 commit comments

Comments
 (0)