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:
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. |
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