$topLvlMgmtGrp = "CHANGETHIS" # Name of the top level management group
$subscriptions = @() # Output array
# Collect data from managementgroups
$mgmtGroups = Get-AzManagementGroup -GroupId $topLvlMgmtGrp -Expand -Recurse
$children = $true
while ($children) {
$children = $false
$firstrun = $true
foreach ($entry in $mgmtGroups) {
if ($firstrun) {Clear-Variable mgmtGroups ; $firstrun = $false}
if ($entry.Children.length -gt 0) {
# Add management group to data that is being looped throught
$children = $true
$mgmtGroups += $entry.Children
}
else {
# Add subscription to output object
$subscriptions += New-Object -TypeName psobject -Property ([ordered]@{'DisplayName'=$entry.DisplayName;'SubscriptionID'=$entry.Name})
}
}
}
$subscriptions
Add the following to remove the managemnet groups from the list and only show the subs:
elseif ($entry.Type -eq “/subscriptions”) {
# Only add to subscriptions if it’s actually a subscription
$subscriptions += New-Object -TypeName psobject -Property ([ordered]@{
‘DisplayName’ = $entry.DisplayName
‘SubscriptionID’ = $entry.Name
})
}