You like to offboard a mailbox from Exchange Online to Exchange On-Premises. This time you…
Export Microsoft 365 group members to CSV with PowerShell
There are many groups in the organization, and we like to know which members are added to each group. We can manually go through the groups and write them down, but that will take ages. Another method is to use PowerShell to speed up the process. In this article, you will learn how to export Microsoft 365 group members to CSV with PowerShell.
Table of contents
Microsoft 365 group types
There are 4 different group types in Microsoft 365, and we want to export all of these groups, including their members.
1. Microsoft 365
Allows teams to collaborate by giving them a group email and a shared workspace for conversations, files, and calendars. In Outlook, these are called Groups.
2. Distribution List
Creates an email address for a group of people.
3. Dynamic distribution list
Sends email to all members of the list. The group’s membership list is updated every 24 hours, based on the filters and conditions you set.
4. Mail-enabled security
Sends messages to all members of the group and gives access to resources like OneDrive, SharePoint and admin roles
Find Microsoft 365 groups
To find all the groups in Microsoft 365, follow these steps:
- Sign in to Microsoft Exchange admin center
- Click Recipients > Groups
- Click on the group type to list the groups
Let’s look at the next step on how to export Microsoft 365 group members with PowerShell.
Get Microsoft 365 group members with PowerShell script
The Export-M365GroupMembers.ps1 PowerShell will get all the Microsoft 365 group types, including their members, and export them to a CSV file.
For every Microsoft 365 group, it gathers the following information:
- GroupId
- GroupDisplayName
- GroupType
- UserDisplayName
- UserPrincipalName
- UserAlias
- UserType
- UserAccountEnabled
Note: The script will export all the Microsoft 365 group types. If there are no group members in a specific group, it will show as N/A (not applicable).
1. Install Microsoft Graph PowerShell module
Before we can proceed further and get the Microsoft 365 group members from all the groups, we need to Install the Microsoft Graph PowerShell module.
Start Windows PowerShell as administrator and run the below commands.
Install-Module Microsoft.Graph -Force
Install-Module Microsoft.Graph.Beta -AllowClobber -Force
Important: Always install the Microsoft Graph PowerShell and Microsoft Graph Beta PowerShell modules. That’s because some cmdlets are not yet available in the final version, and they will not work. Update both modules to the latest version before you run a cmdlet or script to prevent errors and incorrect results.
2. Prepare Export Microsoft 365 group members PowerShell script
Create two folders on the (C:) drive:
- Temp
- Scripts
Download the Export-M365GroupMembers.ps1 PowerShell script and place it in C:\scripts folder. The script will export the CSV file to the C:\temp folder.
Ensure the file is unblocked to prevent errors when running the script. Read more in the article Not digitally signed error when running PowerShell script.
Another option is to copy and paste the code below into Notepad. Give it the name Export-M365GroupMembers.ps1 and place it in the C:\scripts folder.
<#
.SYNOPSIS
Export-M365GroupMembers.ps1
.DESCRIPTION
Export Microsoft 365 Group Members to CSV file with PowerShell.
.LINK
www.alitajran.com/export-microsoft-365-group-members-to-csv-powershell/
.NOTES
Written by: ALI TAJRAN
Website: www.alitajran.com
LinkedIn: linkedin.com/in/alitajran
.CHANGELOG
V1.00, 03/18/2024 - Initial version
#>
# CSV file path to export
$CsvPath = "C:\temp\M365GroupMembers.csv"
# Connect to Microsoft Graph with specified scopes
Connect-MgGraph -Scopes "User.Read.All", "Group.Read.All"
# Retrieve all groups
$groups = Get-MgGroup -All
# Get properties
$Properties = @(
'Id', 'DisplayName', 'UserPrincipalName', 'UserType', 'AccountEnabled'
)
# Initialize an array to store user information
$allUsers = @()
# Set up the progress bar parameters
$totalGroups = $groups.Count
$currentGroup = 0
# Iterate through each group and retrieve group members
foreach ($group in $groups) {
# Retrieve group members using the valid Group ID
$members = Get-MgGroupMember -GroupId $group.id -All
# Determine the group type
$groupType = if ($group.groupTypes -eq "Unified" -and $group.securityEnabled) { "Microsoft 365 (security-enabled)" }
elseif ($group.groupTypes -eq "Unified" -and !$group.securityEnabled) { "Microsoft 365" }
elseif (!($group.groupTypes -eq "Unified") -and $group.securityEnabled -and $group.mailEnabled) { "Mail-enabled security" }
elseif (!($group.groupTypes -eq "Unified") -and $group.securityEnabled) { "Security" }
elseif (!($group.groupTypes -eq "Unified") -and $group.mailEnabled) { "Distribution" }
else { "N/A" }
# If there are no members, create an object with empty values
if ($members.Count -eq 0) {
$Objects = [PSCustomObject][ordered]@{
GroupId = $group.Id
GroupDisplayName = $group.DisplayName
GroupType = $groupType
UserDisplayName = "N/A"
UserPrincipalName = "N/A"
UserAlias = "N/A"
UserType = "N/A"
UserAccountEnabled = "N/A"
}
$allUsers += $Objects
}
else {
# Iterate through each group member and retrieve user details
foreach ($member in $members) {
$user = Get-MgUser -UserId $member.Id -Property $Properties -ErrorAction SilentlyContinue | Select-Object $Properties
# Check if $user is not null before accessing properties
if ($user.Count -ne 0) {
# Extract the alias from the UserPrincipalName
$alias = $user.UserPrincipalName.Split("@")[0]
# Create an ordered custom object with properties in a specific order
$Objects = [PSCustomObject][ordered]@{
GroupId = $group.Id
GroupDisplayName = $group.DisplayName
GroupType = $groupType
UserDisplayName = $user.DisplayName
UserPrincipalName = $user.UserPrincipalName
UserAlias = $alias
UserType = $user.UserType
UserAccountEnabled = $user.AccountEnabled
}
# Add the ordered custom object to the array
$allUsers += $Objects
}
}
}
# Update the progress bar
$currentGroup++
$status = "{0:N0}" -f ($currentGroup / $totalGroups * 100)
$progressParams = @{
Activity = "Retrieving Group Members"
Status = "Processing group: $($group.DisplayName) - $currentGroup of $totalGroups : $status% completed"
PercentComplete = ($currentGroup / $totalGroups) * 100
}
Write-Progress @progressParams
}
# Complete the progress bar
Write-Progress -Activity "Retrieving Group Members" -Completed
# Export all user information to a CSV file
$allUsers | Sort-Object GroupDisplayName | Export-Csv $CsvPath -NoTypeInformation -Encoding utf8
- Line 21: Edit the CSV file path
3. Run Export Microsoft 365 group members PowerShell script
Get all the Microsoft 365 groups, including their members, and export them to a CSV file.
Run the below command to run the script Export-M365GroupMembers.ps1.
c:\scripts\.\Export-M365GroupMembers.ps1
4. Open Microsoft 365 group members report
The Export-M365GroupMembers.ps1 PowerShell script exports all the Microsoft 365 group members to a CSV file.
Find the file M365GroupMembers.csv in the path C:\temp.
Open the CSV file with your favorite application. In our example, it’s Microsoft Excel.
The Microsoft 365 group members report looks great.
Read more: Export Microsoft 365 disabled users report »
Conclusion
You learned how to export Microsoft 365 group members to CSV with PowerShell. While you can export the groups from the admin center, it doesn’t get the group members. You must use PowerShell to create a custom Microsoft 365 group report.
Did you enjoy this article? You may also like Block sign-in from shared mailboxes. Don’t forget to follow us and share this article.
This Post Has 0 Comments