Office 365 Group is now branded as Microsoft 365 Group (aka Unified Group) which is a back-end service for other Microsoft 365 services such as Microsoft Teams, Planner, etc. In this post, we will share Powershell script to find and export group members.
Get Office 365 Group Members and Owners
We can use the Azure AD Powershell command Get-AzureADGroupMember to get members of a group. Before start, install the Azure AD PowerShell module and run the following to connect the module.
Connect-AzureAD
Run the below command to get a list of group members.
#Get the Group object
$Group = Get-AzureADGroup -SearchString "<GroupName>"
#Retrive Group Members
Get-AzureADGroupMember -ObjectId $Group.ObjectId -All $true
We can use the Get-AzureADGroupOwner cmdlet to get group owners.
$Group = Get-AzureADGroup -SearchString "<GroupName>"
Get-AzureADGroupOwner -ObjectId $Group.ObjectId -All $true
Export Office 365 Group Members
Run the following commands to export group member details to CSV file. Replace the “<GroupName>” with your group name.
$Group = Get-AzureADGroup -SearchString "<GroupName>"
Get-AzureADGroupMember -ObjectId $Group.ObjectId -All $true | Select DisplayName, UserPrincipalName |
Export-CSV "C:\GroupMembers.csv" -NoTypeInformation -Encoding UTF8
Along with DisplayName, you can also select more user properties such as Mail, AccountEnabled, UserType (indicates guest or external user), AssignedLicenses, JobTile, and more. You can select all properties that are supported in the Get-AzureADUser cmdlet.
Export All Office 365 Groups and their Members
We can use the Get-AzureADMSGroup cmdlet to get all Microsoft 365 groups. Run the following commands to export all office 365 groups and members to a CSV file.
$Result = @()
$AllGroups = Get-AzureADMSGroup -Filter "groupTypes/any(c:c eq 'Unified')" -All $true
$TotalGroups = $AllGroups.Count
$i = 1
ForEach ($Group in $AllGroups) {
Write-Progress -Activity "Processing $($Group.DisplayName)" -Status "$i out of $TotalGroups groups completed"
#Get group members
$GroupMembers = Get-AzureADGroupMember -ObjectId $Group.Id -All $true
ForEach ($User in $GroupMembers) {
$Result += New-Object PSObject -property $([ordered]@{
GroupName = $Group.DisplayName
GroupMail = $Group.Mail
UserName = $User.DisplayName
UserPrincipalName = $User.UserPrincipalName
AccountEnabled = $User.AccountEnabled
JobTitle = $User.JobTitle
IsGuestUser = if ($User.UserType -eq 'Guest') { $true } else { $false }
IsLicensed = if ($User.AssignedLicenses.Count -ne 0) { $true } else { $false }
})
}
$i++
}
$Result | Export-CSV "C:\AllGroupMembers.CSV" -NoTypeInformation -Encoding UTF8
The above commands store the output details in the array object $Result, we can generate more reports by applying a filter in the $Result array using available fields.
Get all guest or external users
The below command gets a list of all guest users in all groups.
$Result | Where-Object { $_.IsGuestUser -eq $true } | Select GroupName, UserName
Export all guest group members
$Result | Where-Object { $_.IsGuestUser -eq $true } | Export-CSV "C:\GuestMembers.CSV" -NoTypeInformation -Encoding UTF8
Get unlicensed members in all groups
$Result | Where-Object {$_.IsLicensed -eq $false} | Select GroupName, UserName
Find disabled members in all groups
$Result | Where-Object {$_.AccountEnabled -eq $false} | Select GroupName, UserName
Get Office 365 Group Members using Exchange Online Powershell module
You can use the Get-UnifiedGroupLinks cmdlet to get members and owners of Microsoft 365 groups.
#Connect Exchange Online module
Connect-ExchangeOnline
#Get members
Get-UnifiedGroupLinks -Identity '<group-name>' -LinkType Members -ResultSize Unlimited