How To Get Members Of M365 Group

Introduction

Recently, there has been a requirement to get users inside the M365 groups. The use case here is to get the members list with access to the SharePoint site created as part of Teams. The permissions for the Team sites, created as part of MSFT Teams creation, are managed from M365 groups. If we need to get Members inside the M365 group, there are 2 ways. 

Via Azure Portal

You can view the Microsoft 365 group members through Azure Active Directory groups.  

Step 1

Login to https://portal.azure.com and click on Azure Active Directory. You can also access the Azure AD directly by link https://aad.portal.azure.com  

How To Get Members Of M365 Group

Step 2

Click on 'Groups'.  

How To Get Members Of M365 Group

Step 3

Search for the group where you need to get the members of it. In this case, my group name is 'Demo Vinay'.  

How To Get Members Of M365 Group

Step 4

Click on 'Members'.

How To Get Members Of M365 Group

How To Get Members Of M365 Group

Using PnP PowerShell

Step 1

Connect to PnP PowerShell using 'SharePoint Admin' rights.  

$AdminSiteURL = https://contoso-admin.sharepoint.com 
$creds = Get-Credential -UserName "[email protected]" -Message "Logging into SharePoint admin to get groups information" 
Connect-PnPOnline -Url $AdminSiteURL -Credentials $creds 

Step 2

Get the M365 Group Names in CSV as per the below screenshot. If you want to get the group names via PowerShell, please refer to the references section.  

How To Get Members Of M365 Group

#Import the csv 
$Groups = Import-Csv -Path "C:\GroupInfo\Inputs\M365Groups.csv" 

Step 3

Get the members of the group for each group name.         

$GroupInfo = Get-PnPMicrosoft365GroupMembers -Identity $Group
#Get Members of the group and then join the multiple members with semi colon.
$GroupMembers = ($GroupInfo | Select -ExpandProperty UserPrincipalName) -join ";"

Complete Script

#Config Variables
$CurrentDate = Get-Date 
$DateFormat = $CurrentDate.ToString('MM-dd-yyyy_hh-mm-ss')
$AdminSiteURL = "https://contoso-admin.sharepoint.com"
$CSVPath = "C:\GroupInfo\Outputs\GroupMembersInfo-"+$DateFormat+".csv"
$creds = Get-Credential -UserName "[email protected]" -Message "Logging into SharePoint admin to get groups information"
 
Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $AdminSiteURL -Credentials $creds
 
    #Get all Office 365 Groups. Update the path accordingly
    $Groups = Import-Csv -Path "C:\GroupInfo\Inputs\M365Groups.csv"
     
    $GroupsData = @()
    #Loop through each group
    ForEach($Group in $Groups.M365Group)
    {
        Write-host "Processing Group:" $Group -ForegroundColor Yellow
        $GroupInfo = Get-PnPMicrosoft365GroupMembers -Identity $Group
        #Get Members of the group and then join the multiple members with semi colon. 
        $GroupMembers = ($GroupInfo | Select -ExpandProperty UserPrincipalName) -join ";"
 
        #Get Group details by creating PS Custom object so that it can be exported to csv
        $GroupsData += New-Object PSObject -property $([ordered]@{ 
            GroupName  = $Group            
            GroupMembers= $GroupMembers
        })
    }
    $GroupsData
    #Export Groups information to CSV
    $GroupsData | Export-Csv -Path $CSVPath -NoTypeInformation
}
Catch {
    write-host -f Red "Error:" $_.Exception.Message
}

References


Similar Articles