SharePoint Online: PowerShell to Get Group Members

Requirement: Get SharePoint Online Group Members using PowerShell

How to Get Group Members in SharePoint Online?

If you’re a SharePoint administrator, you know that there’s a lot of work involved in managing permissions. Whether on-premise or online, it can be challenging to track who has access to what and when they have rights to the content. Are you looking for a way to get group members of the SharePoint Online site? If so, this blog post shows you how to get group members in SharePoint Online. Let’s get started!

To get users in a group in SharePoint Online, follow these steps:

  • Go to Site Settings >>  People and Groups
  • Pick the SharePoint Online group you want to get group members.
  • and the group page gets you all users in a particular group. get sharepoint online group members powershell

Let’s see the SharePoint Online PowerShell to list group members.

SharePoint Online: PowerShell to Get Group Members

If you want to get a list of group members in SharePoint Online, you can use PowerShell. This will give you a quick and easy way to get the information you need. Using this PowerShell script lets you easily get a list of all the members in a group and get specific details on each member, such as their login ID or title. Whether you need to quickly gather information about your groups or just want an easy way to get members of each group, PowerShell is the way to go.

Here is the SharePoint Online PowerShell to get users in a group:

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#Config Variables
$SiteURL="https://Crescent.sharepoint.com/sites/marketing"
$GroupName= "Team Site Members"

Try {
    $Cred= Get-Credential
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
 
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Credentials
    
    #Get the Group & Members of the group
    $Group = $Ctx.web.SiteGroups.GetByName($GroupName)
    $Ctx.Load($Group)
    $GroupUsers = $Group.Users
    $Ctx.Load($GroupUsers)
    $Ctx.ExecuteQuery()

    #Iterate through each User of the Group
    ForEach($User in $GroupUsers)
    {
        #get sharepoint online group members powershell
        $User | Select Title, Email
    }
}
Catch {
    write-host -f Red "Error Getting Group Users!" $_.Exception.Message
}

This script gets users in a group.

PowerShell to Get Group members with SharePoint Online Management Shell

We can use the Get-SPOUser cmdlet to get users in a group. Here is the PowerShell to list members of the SharePoint Online group:

#Config Variables
$AdminCenterURL = "https://Crescent-admin.sharepoint.com/"
$SiteURL="https://Crescent.sharepoint.com/sites/marketing"
$GroupName= "Team Site Members"

#Connect to SharePoint Online
Connect-SPOService -url $AdminCenterURL -Credential (Get-Credential)
  
#sharepoint online powershell get group members
Get-SPOUser -Site $SiteURL -Group $GroupName
sharepoint online powershell get group members

SharePoint Online PowerShell to Export Group Members

Here is how to export group members to Excel:

Get-SPOUser -Site $SiteURL -Group $GroupName | Select DisplayName, LoginName | Export-csv -NoTypeInformation -Path "C:\temp\GroupUsers.csv"

If you need to get all groups and users in groups, use: SharePoint Online: Users and Groups Report using PowerShell

PnP PowerShell to Get Group Members

You can also use Get-PnPGroupMember cmdlet to get members of the group using PnP PowerShell.

#Parameter
$SiteURL= "https://crescent.sharepoint.com/sites/marketing"
$GroupName = "Marketing Team Site Members"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#sharepoint online pnp powershell get group members
Get-PnPGroupMember -Identity $GroupName

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

Leave a Reply

Your email address will not be published.