Forum Discussion

SS_9711's avatar
SS_9711
Copper Contributor
May 19, 2023

Download group members with groups they belong to (AAD)

Hello,

Just like in topic - I need to download list of users from AAD with groups they belong to, for example:

Group A has users X, Y and Z and X is a member of group 1, Y - 1 and 3, Z - 2, 3. I need a list of them with their groups.

I always used script below to get users from group but I dont have any idea to create script which will help me complete my sentence.

 

Connect-AzureAD
$GroupName = Read-Host -Prompt "Enter group name: "
$Groups = Get-AzureADGroup -SearchString $GroupName
$GroupCount = $groups | measure
$Count = $groupCount.Count


$GroupMembers = foreach($Group in $Groups){
$GroupID = $Group.ObjectId
$GroupName = $Group.DisplayName
Write-Progress -Activity "Groups found: $Count`
Group name: $GroupName"
Get-AzureADGroupMember -ObjectId $GroupID | Select-Object -Property @{Name = 'GroupName'; Expression= {$GroupName}}, UserPrincipalName
}

$GroupMembers | Export-Csv C:\PSData\AADGroup.csv

 

I hope I explained everything correctly. 🙂

Resources