How to Get Office 365 Group Members using PowerShell?
Requirement: Get Members of Office 365 Group using PowerShell.
How to Get Office 365 Group Members?
Office 365 Groups offer a convenient way to collaborate with others in the organization. If you need to get a list of all the group members, PowerShell makes it easy. This guide will show you how to use PowerShell to get the list of users in an Office 365 Group. Let’s get started!
To get a list of members in the Office 365 group from Microsoft 365 admin center, do the following:
- Log in to the Microsoft 365 Admin Center site: https://admin.microsoft.com
- Expand “Teams & Groups” and Click on “Active Teams & Groups” in the left navigation.
- Search and click on the Office 365 group you wish to get members from.
- On the members tab, click on “Members”. This lists all members of the group.
PowerShell to Get Group Members in Office 365:
Here is the Office 365 PowerShell get group members with Get-UnifiedGroup and Get-UnifiedGroupLinks cmdlets.
#Connect to Exchange Online
Connect-ExchangeOnline -ShowBanner:$False
#Get all Members of Office 365 Group
Get-UnifiedGroup -Identity "Marketing@TheCrescentTech.com" | Get-UnifiedGroupLinks -LinkType Member
#Disconnect Exchange Online
Disconnect-ExchangeOnline -Confirm:$False
You can export all Office 365 group members to CSV as:
#Get all Members of Office 365 Group and export to CSV
Get-UnifiedGroup -Identity "Marketing@TheCrescentTech.com" | Get-UnifiedGroupLinks -LinkType Member `
| Select DisplayName,PrimarySmtpAddress | Export-CSV "C:\Temp\GroupMembers.csv" -NoTypeInformation
Export Members from All Office 365 Groups to CSV using PowerShell
Ever needed to get a list of all the members in a Microsoft 365 group and export them to a CSV file? Sure, We can use PowerShell to quickly retrieve a list of all the members in groups. Here is the PowerShell to export all members of all Office 365 groups:
$CSVPath = "C:\Temp\AllGroupMembers.csv"
#Connect to Exchange Online
Connect-ExchangeOnline -ShowBanner:$False
#Remove the CSV file if exists
If(Test-Path $CSVPath) { Remove-Item $CSVPath}
#Get All Office 365 Groups
$O365Groups=Get-UnifiedGroup
ForEach ($Group in $O365Groups)
{
Write-Host "Group Name:" $Group.DisplayName -ForegroundColor Green
Get-UnifiedGroupLinks -Identity $Group.Id -LinkType Members | Select DisplayName,PrimarySmtpAddress
#Get Group Members and export to CSV
Get-UnifiedGroupLinks -Identity $Group.Id -LinkType Members | Select-Object @{Name="Group Name";Expression={$Group.DisplayName}},`
@{Name="User Name";Expression={$_.DisplayName}}, PrimarySmtpAddress | Export-CSV $CSVPath -NoTypeInformation -Append
}
#Disconnect Exchange Online
Disconnect-ExchangeOnline -Confirm:$False
PnP PowerShell to Get Members of an Office 365 Group
Here is how to get Office 365 group members using the Get-PnPMicrosoft365GroupMembers cmdlet.
#Config Variables
$AdminSiteURL = "https://crescent-admin.sharepoint.com"
$GroupEmail = "HRAdmin@crescent.com"
Try {
#Connect to PnP Online
Connect-PnPOnline -Url $AdminSiteURL -Interactive
#Get the Office 365 Group from Email
$Group = Get-PnPMicrosoft365Group | Where Mail -eq $GroupEmail
If($Group -ne $Null)
{
#Get Office 365 group members
$Group | Get-PnPMicrosoft365GroupMembers
}
Else
{
Write-host "Could not Find Group!" -f Yellow
}
}
Catch {
write-host -f Red "Error:" $_.Exception.Message
}
Export All Members of All Groups to a CSV
How about exporting members of all groups from the Microsoft 365 environment?
#Config Variables
$AdminSiteURL = "https://crescent-admin.sharepoint.com"
$CSVPath = "C:\Temp\GroupsMembersData.csv"
Try {
#Connect to PnP Online
Connect-PnPOnline -Url $AdminSiteURL -Interactive
#Get all Office 365 Groups
$Groups = Get-PnPMicrosoft365Group
$GroupsData = @()
#Loop through each group
ForEach($Group in $Groups)
{
Write-host "Processing Group:"$Group.DisplayName
#Get Members of the group
$GroupMembers = (Get-PnPMicrosoft365GroupMembers -Identity $Group | Select -ExpandProperty UserPrincipalName) -join ";"
#Get Group details
$GroupsData += New-Object PSObject -property $([ordered]@{
GroupName = $Group.DisplayName
Id = $Group.ID
Visibility = $Group.Visibility
Mail = $Group.Mail
GroupMembers= $GroupMembers
})
}
$GroupsData
#Export Groups information to CSV
$GroupsData | Export-Csv -Path $CSVPath -NoTypeInformation
}
Catch {
write-host -f Red "Error:" $_.Exception.Message
}
Get Office 365 Group Members using Azure AD PowerShell Module
You can also list group users using Azure AD PowerShell as:
#Get Credentials to connect
$Cred = Get-Credential
#Connect to AzureAD
Connect-AzureAD -Credential $Cred
#Get the Group
$Group = Get-AzureADGroup -Filter "DisplayName eq 'HR Team'"
#Get Group Members
Get-AzureADGroupMember -ObjectId $Group.ObjectId
How about exporting Azure AD Group members to a CSV?
PowerShell to Get Azure AD Group members and Export to CSV
#Connect to AzureAD
Connect-AzureAD | Out-Null
#Get the Group
$Group = Get-AzureADGroup -Filter "DisplayName eq 'Sales Managers'"
#Get Group Members
Get-AzureADGroupMember -ObjectId $Group.ObjectId | Select DisplayName, UserPrincipalName, Mail | Export-Csv -path "C:\Temp\GroupMembers.csv" -Notypeinformation
Conclusion:
This guide has shown you two ways to retrieve a list of members for an Office 365 Group using the Microsoft Admin center and SharePoint Online PowerShell module. Both methods are easy to use and will allow you to quickly retrieve a list of members for a specific group, including their display name and email address. You can also export the data to a CSV file.
I’ll ask here because Microsoft support was unable to help me. I have a mixed bag here. My SharePoint Online has a combination of Site Owners in for of individuals (shown by email address), Site Groups and o365 Groups. So If I run one of your nice reports, I’ll get the details of the Site Owners and I think the Site Group owner too, but the row with groups and does not mention members, are o365 groups that are the Site Owner. Do you have a combo script that would allow me to get the Site name, Permissions, Site Owner, Site Group Ower and o365 Group Members (because they would be the site owner group)?
Thank you for this. Can is be easily scoped to list all groups a single user is a member of?
Here you go! Find All Office 365 Group Memberships of a User using PowerShell
Same error here: “”A positional parameter cannot be found that accepts argument ‘Members’.” It’s something to do with the For loop. If you just run
Get-UnifiedGroupLinks “Identity GroupName “LinkType Members | Select DisplayName,PrimarySmtpAddress
it works fine.
What about if I wish to export users who are assigned with Office E1 SKU ID: StandardPack
Thank You for Share your experience!
do you have any suggestions if PS timeouts due to MFA .. am getting incomplete output.
Remove the -Credential parameter! Then you’ll be prompted to enter credentials in a popup window, which is MFA aware! So, instead of “Connect-AzureAD -Credential $Cred”, just use: Connect-AzureAD
“A positional parameter cannot be found that accepts argument ‘Members’.” on each site when trying to create the CSV.
I’m getting the same issue. Did you find a resolution?
I believe that it is just a typo. I think it should be Member not Members
Did you try it? That’s not the issue. Official documentation has Members.
The other comments are wrong. The problem is they hyphens in front of LinkType and also Identity. They’re not real hyphens. remove them and replace them with real hyphens.
thank you! I was slamming my head against a wall on that
Your blog has been a tremendous resource for us!