Powershell - List subscriptions of a specific management group

Viewed 2635

On Azure and with Powershell, I need to list all the subscriptions that are in a specific management group. The command Get-AzSubscription has no parameter to filter on a specific management group. And there is no powershell command (AzManagementGroup) either to list the subscriptions inside.

I was thinking about creating an msgraph query to do that and call it from powershell, but perhaps there is an easier way to do that? :)

3 Answers

The PowerShell Cmdlet you would want to use is Get-AzManagementGroup. This is how you would use it:

$response = Get-AzManagementGroup -GroupName TestGroupParent -Expand -Recurse

Child subscriptions and management groups can be accessed via Children property. Something like:

$response.Children[0]

The correct way to, recursively, fetch the subscriptions under a given management group is the following:

Search-AzGraph -Query "ResourceContainers | where type =~ 'microsoft.resources/subscriptions'" -ManagementGroup $managementGroupName

The code above expects the $managementGroupName variable to contain the name of the management group

Search-AzGraph -Query "ResourceContainers | where type =~ 'microsoft.resources/subscriptions'" -ManagementGroup $managementGroupName -First 200 | Format-Table -Property *

The query was good but AZGraph limits you to first 100 results only just changed this a bit to include first 200.

Related