How to Get Microsoft 365 Group Members Using PowerShell?

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:

Get Office 365 Group Members using PowerShell

I want to get these Office 365 Group members using PowerShell [Windows PowerShell ISE], as shown below.

Output:

how to get office 365 group members using powershell

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.

get group members powershell office 365

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.

get security group members powershell office 365

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
get group members from office 365 in powershell

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
get user group membership powershell office 365

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.

how to get user group membership powershell office 365

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.
Export Microsoft 365 group members to CSV with PowerShell

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:

Export Members from All Office 365 Groups to CSV using PowerShell

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:

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.

>