Microsoft 365 administrators often need to retrieve the list of members from distribution groups to manage users effectively. While the Microsoft Admin Center provides a graphical way to view group memberships, PowerShell provides a more efficient and automated approach. In this article, we will explore how to fetch members of a distribution group using Microsoft Graph PowerShell.
Below is the PowerShell script that retrieves and displays the Group Name, Group Email, Member Name, Department, and Job Title of each member in a distribution group.
# Install & Import Graph Module (if not already installed)
# Install-Module Microsoft.Graph -Scope CurrentUser
Import-Module Microsoft.Graph
# Connect to Microsoft Graph
Connect-MgGraph -Scopes "Group.Read.All", "User.Read.All"
# Prompt for Distribution Group Email
$GroupEmail = Read-Host "Enter Distribution Group Email"
# Get Group Details
$Group = Get-MgGroup -Filter "mail eq '$GroupEmail'" -Property Id, DisplayName, Mail
if ($Group -eq $null) {
Write-Host "Error: No group found with email '$GroupEmail'" -ForegroundColor Red
exit
}
# Fetch Group Members (Only returns IDs, so we need to query user details separately)
$Members = Get-MgGroupMember -GroupId $Group.Id -All
if ($Members.Count -eq 0) {
Write-Host "No members found in the group '$($Group.DisplayName)'" -ForegroundColor Yellow
} else {
# Retrieve user details for each member
$MemberDetails = @()
foreach ($Member in $Members) {
$User = Get-MgUser -UserId $Member.Id -Property DisplayName, Department, JobTitle
$MemberDetails += [PSCustomObject]@{
"Group Name" = $Group.DisplayName
"Group Email" = $Group.Mail
"Member Name" = $User.DisplayName
"Department" = $User.Department
"Job Title" = $User.JobTitle
}
}
# Display Results in Table Format
$MemberDetails | Format-Table -AutoSize
}
# Disconnect from Graph
Disconnect-MgGraph
Here are some possible improvements that can be made to the script:
You can identify distribution groups by checking the GroupTypes property. Distribution groups have an empty GroupTypes value, whereas Microsoft 365 groups have GroupTypes set to "Unified".
Get-MgGroupMember return members from nested groups?No, by default Get-MgGroupMember only retrieves top-level members. To list all users from nested groups, you need to run recursive queries for each child group ID.
Yes. After retrieving members using Get-MgGroupMember, you can pipe the output to Export-Csv, for example:
Get-MgGroupMember -GroupId <GroupId> -All | Select-Object DisplayName, Mail | Export-Csv "DGMembers.csv" -NoTypeInformation
You can check the UserType property after fetching member details with Get-MgUser. Users with UserType set to "Guest" are external members.
| Error | Cause | Solution |
| Error: No group found with email | The entered email does not match any distribution group. | Verify the email address and try again. |
| No members found in the group | The group is empty or the members are not retrievable. | Ensure the group has members. Check if the members are external contacts. |
| Get-MgUser: User Not Found | The script tries to fetch details for a non-user object (e.g., a service principal). | Modify the script to filter only User objects from the group members. |
| Access Denied | The connected account lacks permissions. | Ensure the Graph API permissions Group.Read.All and User.Read.All are granted and admin consented. |
When you run Get-MgGroupMember -GroupId <Id> -All on a distribution group (where GroupTypes is empty), you can quickly analyze membership size. Smaller groups often pose a risk of over-privileged users—ideal for periodic audit checks.
When distribution groups contain other groups as members, Get-MgGroupMember returns only the top-level entries. To resolve nested memberships, you’ll need to recursively call the cmdlet for each child group. This ensures you capture all individual users who indirectly belong to the parent distribution list.
This PowerShell script offers an efficient way to fetch and display members of a Microsoft 365 distribution group, including critical details like Member Name, Department, and Job Title. By leveraging Microsoft Graph API, administrators can automate user audits and gain better visibility into group memberships. Further enhancements such as exporting to CSV or bulk processing multiple groups can make this script even more powerful.
© m365corner.com. All Rights Reserved. Design by HTML Codex