Listing Groups, Owners, and Members from Microsoft 365 with Powershell
Why Do They Need To Make This So Difficult?
A client requested a list of groups in Microsoft 365, members, and the owners/managers of those groups. My first impulse was to use Get-MsolGroup
and Get-MsolGroupMember
, but I quickly realized that it didn’t return the owner. So I had to go to a completely different module and compile two different list of groups. Microsoft just can’t make this easy.
Code Time
Import-Module ExchangeOnlineManagement
Connect-ExchangeOnline
$DistroGroups = Get-DistributionGroup
$UnifiedGroups = Get-UnifiedGroup
$DistroGroupMembers = $DistroGroups | ForEach-Object{
[PSCustomObject]@{
Name = $_.DisplayName
Type = "DistroGroup"
ManagedBy = $_.ManagedBy
Members=(Get-DistributionGroupMember -Identity $_.SamAccountName).Name -join ", "
}
}
$UnifiedGroupMembers = $UnifiedGroups | ForEach-Object{
[PSCustomObject]@{
Name = $_.DisplayName
Type = "Microsoft 365 Group"
ManagedBy = $_.ManagedBy
Members=($_ | Get-UnifiedGroupLinks -linktype Members).Name -join ", "
}
}
$DistroGroupMembers + $UnifiedGroupMembers | Export-CSV -path "$([Environment]::GetFolderPath('MyDocuments'))\Group Report $(get-date -Format "yyyyMMdd").csv" -NoTypeInformation
Bonus Extra Credit
Here’s something new. I’ve decided to throw in some extra credit. If you are running this at home, try throwing in Out-GridView into middle of the output so you can manually filter what gets saved int the report.