đź”§ New: User Management Graph PowerShell Toolkit

Simplify user tasks like bulk creation, updates, password resets, deletions, license checks & more — all from one place.

🚀 Launch Toolkit

Email Microsoft 365 Group Members to Admin using Graph PowerShell

Need a quick way to fetch the members of a Microsoft 365 group and email them to an administrator? This Graph PowerShell script does exactly that — it retrieves the members of a specified group, constructs an HTML table, and sends the member list directly to the admin’s inbox.


Script – Mail Microsoft 365 Group Members to Admin


    # Connect to Microsoft Graph
    Connect-MgGraph -Scopes "Group.Read.All", "User.Read.All", "Mail.Send"

    # Define the group and admin
    $groupId = "7ac400b1-72fe-47a4-b437-57671ca08f86"  # Replace with your group's ID
    $adminEmail = "samadmin@7xh7fj.onmicrosoft.com"  # Replace with the admin's email

    # Get group details
    $group = Get-MgGroup -GroupId $groupId
    $groupName = $group.DisplayName

    # Get group members
    $members = Get-MgGroupMember -GroupId $groupId -All

    # Build HTML table
    $htmlTable = "<html><body>"
    $htmlTable += "<h3>Group Name: $groupName</h3>"
    $htmlTable += "<table border='1' cellpadding='5' cellspacing='0'>
        "
        $htmlTable += "
        <tr><th>Name</th><th>Email</th></tr>"

        foreach ($member in $members) {
        $name = $member.AdditionalProperties.displayName
        $email = $member.AdditionalProperties.mail
        $htmlTable += "
            <tr><td>$name</td><td>$email</td></tr>"
        }
        $htmlTable += "
    </table>
    </body></html>"
                                
    # Create email body
    $emailBody = @{
        Message = @{
        Subject = "Member List for Group: $groupName"
        Body = @{
            ContentType = "HTML"
            Content = $htmlTable
        }
        ToRecipients = @(
        @{
            EmailAddress = @{
                Address = $adminEmail
           }
        }
        )
        }
        SaveToSentItems = $true
    }

    # Send the email
    Send-MgUserMail -UserId $adminEmail -BodyParameter $emailBody
                            


How the Script Works

  1. Connects to Microsoft Graph
  2. The script starts by connecting to Microsoft Graph with the scopes:

    • Group.Read.All: To read group and member details
    • User.Read.All: To resolve user identities
    • Mail.Send: To send the email with the group data
  3. Fetches Group Details and Members
  4. The group is retrieved using its unique Group ID, and the display name is extracted. All members are pulled using Get-MgGroupMember -All.

  5. Builds an HTML Table
  6. The script creates a clean, formatted HTML table showing member display names and emails using AdditionalProperties.

  7. Sends the Email
  8. The constructed table is embedded directly into the email body. The message is sent from the admin's account to their own inbox using Send-MgUserMail.


Further Enhancements

This script is clean and simple, but here are a few upgrades you could make:

  • Export to CSV (optional)
  • In addition to HTML email, export a CSV copy of the members:

    $members | ForEach-Object {
    [PSCustomObject]@{
    Name  = $_.AdditionalProperties.displayName
    Email = $_.AdditionalProperties.mail
    }
    } | Export-Csv -Path "GroupMembers.csv" -NoTypeInformation
                                    
  • Include Role (Owner/Member)
  • Fetch group owners separately using Get-MgGroupOwner and merge both sets into one report.

  • Loop Through All Groups
  • Wrap this logic in a loop to send reports for all groups to the admin.

  • Schedule via Task Scheduler
  • Run this script weekly or monthly for automated group audits.


Possible Errors & Solutions

Error Cause Solution
Unauthorized or Access Denied Missing Graph permissions Use Connect-MgGraph with the required scopes
AdditionalProperties is null Some members are not users Add a type check or skip non-user objects
MailboxNotEnabled Admin account doesn't have a mailbox Ensure samadmin@... has an Exchange Online mailbox
Send-MgUserMail fails silently UserId and From address mismatch Ensure -UserId in Send-MgUserMail matches a valid mailbox

Conclusion

This Graph PowerShell script is a handy solution for quickly retrieving and sharing Microsoft 365 group membership details. With an intuitive HTML report and direct delivery to the administrator’s inbox, it’s ideal for audits, team reviews, or periodic group monitoring.

✉️ With just a Group ID and an admin email, you can automate group visibility with zero fuss.


Graph PowerShell Explorer Widget

20 Graph PowerShell cmdlets with easily accessible "working" examples.


Permission Required

Example:


                


                


                

© m365corner.com. All Rights Reserved. Design by HTML Codex