I was recently assigned the task of obtaining specific Office 365 group members using PowerShell. In this PowerShell tutorial, I will show you how to get Microsoft 365 group members using PowerShell in a simple scenario.
Also, we will discuss how to export Microsoft 365 group members to CSV using PowerShell.
How to Get Microsoft 365 Group Members Using PowerShell
I have an Office 365 group named “Database Team,” This group contains the below members.
Input:
I want to get these Office 365 Group members using PowerShell [Windows PowerShell ISE], as shown below.
Output:
To achieve it, follow the below-mentioned steps. Such as:
1. Open your PowerShell and Install AzureAD Module [If you are not installed] using the below command.
Install-Module -Name AzureAD
2. Then, click the Run button to execute the code, like below.
3. Next, connect to your Azure AD to Run the following command.
Connect-AzureAD
4. Once you execute this command, it will ask you to provide the Microsoft 365 credentials. After that, you will get your Azure AD, as shown below.
5. Next, You need the ObjectId of your Office 365 group. If you know the group’s display name, you can find its ObjectId using the following command.
$group = Get-AzureADGroup -SearchString "Database Team"
Where,
- “Database Team” = Office 365 group name
6. Once you have the group object, you can get its members by using the ObjectId, as shown below.
$groupMembers = Get-AzureADGroupMember -ObjectId $group.ObjectId
7. Next, to display the group members, you can use the below output variable.
$groupMembers | Select-Object DisplayName, UserPrincipalName
8. Finally, you will retrieve and display the members of a specific Office 365 group, showing their display names and email addresses [UserPrincipalName], as shown below.
This is how we can get the Office 365 group members using PowerShell.
Export Microsoft 365 group members to CSV with PowerShell
In this example, we will see how to get all the Office 365 groups using PowerShell. In this case, you can use the Get-UnifiedGroup cmdlet. In the same way, for the group members, you can use the Get-UnifiedGroupLinks cmdlet.
You can use the below PowerShell script on Windows PowerShell ISE to export all Office 365 Group Members to csv.
$Groups = Get-UnifiedGroup -ResultSize Unlimited
$Groups | ForEach-Object {
$group = $_
Get-UnifiedGroupLinks -Identity $group.Name -LinkType Members -ResultSize Unlimited | ForEach-Object {
New-Object -TypeName PSObject -Property @{
Group = $group.DisplayName
Member = $_.Name
EmailAddress = $_.PrimarySMTPAddress
RecipientType= $_.RecipientType
}}} | Export-CSV "C:\Office365GroupMembers.csv" -NoTypeInformation -Encoding UTF8
Note:
The PowerShell Get-UnifiedGroup cmdlet is available only in the cloud-based service.
Finally, execute the PowerShell script by clicking the Run button to export all the Microsoft 365 groups and group members to the CSV file, as shown below.
Output:
This is how we can work with how to export Microsoft 365 group members to CSV with PowerShell.
Some more PowerShell tutorial you may also like:
- Delete All SharePoint List Items using PowerShell
- We couldn’t find the Office 365 group connected to this site
- How to Read CSV into Array in PowerShell
- How to Add Quotes in PowerShell
- How to Use PowerShell Foreach With Examples
- The Term ‘Get-MsolUser’ is Not Recognized As the Name of a Cmdlet
I trust this PowerShell tutorial is useful for you. If you have received a task related to how to get the Office 365 group members, follow the above steps to do it.
I am Bijay a Microsoft MVP (10 times – My MVP Profile) in SharePoint and have more than 17 years of expertise in SharePoint Online Office 365, SharePoint subscription edition, and SharePoint 2019/2016/2013. Currently working in my own venture TSInfo Technologies a SharePoint development, consulting, and training company. I also run the popular SharePoint website EnjoySharePoint.com