SharePoint Online: PowerShell to Get Group Members

Requirement:  Get SharePoint Online Group Members using PowerShell

How to Get Group Members in SharePoint Online? 
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
SharePoint Online: PowerShell to Get Group Members
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://crescenttech.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 the Display Name and Email of the user
        $User | Select Title, Email
    }
}
Catch {
    write-host -f Red "Error Getting Group Users!" $_.Exception.Message
}
This script gets users in group.

PowerShell to Get SharePoint Online Group members with SharePoint Online Management Shell:
We can use Get-SPOUser cmdlet to get users in a group. Here is the PowerShell to list members of SharePoint Online group.
#Config Variables
$AdminCenterURL = "https://crescenttech-admin.sharepoint.com/"
$SiteURL="https://crescenttech.sharepoint.com/sites/marketing"
$GroupName= "Team Site Members"

#Connect to SharePoint Online
Connect-SPOService -url $AdminCenterURL -Credential (Get-Credential)
  
#Call the Get-SPOUser cmdlet to get all users of a group
Get-SPOUser -Site $SiteURL -Group $GroupName
sharepoint online powershell get group members
SharePoint Online: 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
SharePoint Online: PowerShell to Get Group Members SharePoint Online: PowerShell to Get Group Members Reviewed by Salaudeen Rajack on January 27, 2018 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.