Managing distribution groups in Microsoft 365 can be time-consuming, especially when you need to check group details and member counts. Instead of manually clicking through each distribution group in the admin center, this PowerShell script automates the process and retrieves all the required information in one go. With this script, you can quickly fetch distribution group names, emails, mail nicknames, and member counts using Microsoft Graph PowerShell.
# Connect to Microsoft Graph (Uncomment if not connected)
# Connect-MgGraph -Scopes "Group.Read.All"
# Retrieve all groups with relevant properties
$allGroups = Get-MgGroup -All -Property DisplayName, Mail, MailNickname, GroupTypes, Id
# Identify Distribution Groups (GroupTypes is empty/null)
$distributionGroups = $allGroups | Where-Object { $_.Mail -ne $null -and ($_.GroupTypes -eq $null -or $_.GroupTypes.Count -eq 0) }
if ($distributionGroups.Count -eq 0) {
Write-Host "No distribution groups found!" -ForegroundColor Yellow
} else {
# Output Header
Write-Host "`nDistribution Groups Report`n" -ForegroundColor Cyan
Write-Host "-------------------------------------------------------------"
foreach ($group in $distributionGroups) {
# Ensure GroupId is not null or empty
$memberCount = "N/A" # Default value if ID is missing
if (![string]::IsNullOrEmpty($group.Id)) {
try {
# Get member count only if ID is valid
$memberCount = (Get-MgGroupMember -GroupId $group.Id -All).Count
} catch {
Write-Host "Warning: Could not retrieve members for group '$($group.DisplayName)'" -ForegroundColor Red
}
} else {
Write-Host "Warning: Skipping group '$($group.DisplayName)' due to missing ID" -ForegroundColor Red
}
# Output group details
Write-Host "Group Name : $($group.DisplayName)"
Write-Host "Group Mail : $($group.Mail)"
Write-Host "Mail Nick Name : $($group.MailNickname)"
Write-Host "Members (Count) : $memberCount"
Write-Host "-------------------------------------------------------------"
}
}
Step 1: Connecting to Microsoft Graph
Step 2: Retrieving All Groups
Step 3: Filtering Distribution Groups
Step 4: Retrieving Member Count
Step 5: Displaying Results
Modify the script to save the output as a CSV file:
$distributionGroups | Select-Object DisplayName, Mail, MailNickname, @{Name='MemberCount';Expression={(Get-MgGroupMember -GroupId $_.Id -All).Count}} | Export-Csv -Path "DistributionGroups.csv" -NoTypeInformation
Allow filtering groups based on keywords:
$filteredGroups = $distributionGroups | Where-Object { $_.DisplayName -match "Marketing" }
Extend the properties fetched using:
-Property DisplayName, Mail, MailNickname, CreatedDateTime, Description
Error Message | Cause | Solution |
Cannot bind argument to parameter 'GroupId' because it is an empty string. | Some groups might have an empty Id | Ensure Id is not null before calling Get-MgGroupMember. The script already implements this fix. |
Request_UnsupportedQuery | The script initially tried filtering groupTypes directly via -Filter, which Graph API does not support | The filtering is now done in PowerShell instead of using -Filter. |
Warning: Could not retrieve members for group | There might be permission issues | Ensure you have GroupMember.Read.All or Group.Read.All permissions assigned to your Graph PowerShell session. |
No distribution groups found! | The GroupTypes value might be different in your tenant | Run a debug check using `$allGroups |
This Graph PowerShell script provides a reliable method to list distribution groups and count their members. Since GroupTypes for distribution lists is null, we filter them based on this property. The script also ensures robustness by handling missing Id values and avoiding crashes.
Administrators can use this script as-is or enhance it further to suit their needs.
© m365corner.com. All Rights Reserved. Design by HTML Codex