Get All Users and Groups in SharePoint Online Site using PowerShell-CSOM

Requirement: Get All Users and Groups Report in SharePoint Online using PowerShell-CSOM

Get All Users and Groups in SharePoint Online Site using PowerShell

PowerShell Script to Get All Users and Groups Report in SharePoint Online:

This PowerShell script gets all groups and users of each group in a SharePoint online site.

#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"
  
#Set Variables for Site URL
$SiteURL= "https://crescent.sharepoint.com/sites/sales/"

#Setup Credentials to connect
$Cred = Get-Credential
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)

Try {
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Cred

    #Get all Groups
    $Groups=$Ctx.Web.SiteGroups
    $Ctx.Load($Groups)
    $Ctx.ExecuteQuery()

    #Get Each member from the Group
    Foreach($Group in $Groups)
    {
        Write-Host "--- $($Group.Title) --- "

        #Getting the members
        $SiteUsers=$Group.Users
        $Ctx.Load($SiteUsers)
        $Ctx.ExecuteQuery()
        Foreach($User in $SiteUsers)
        {
            Write-Host "$($User.Title), $($User.Email), $($User.LoginName)"
        }
    }
}
Catch {
    write-host -f Red "Error getting groups and users!" $_.Exception.Message
}

PnP PowerShell to Get Groups and Users Report in SharePoint Online:

#Config Variables
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$CSVFile = "C:\Temp\GroupUsers.csv"
  
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-credential)  #-UseWebLogin

#Get All Groups from Site - Exclude system Groups
$Groups = Get-PnPGroup | Where {$_.OwnerTitle -ne "System Account"}
[email protected]()

#Get Group Details
ForEach($Group in $Groups)
{
    #Get Group data
    $GroupData += New-Object PSObject -Property ([ordered]@{
    "Group Name" = $Group.Title
    "Users" = $Group.Users.Title -join "; "
    })
}
$GroupData

#Export Users data to CSV file
$GroupData | Export-Csv -NoTypeInformation $CSVFile

Here is my another article to get all groups and group members from SharePoint online using SharePoint online shell: SharePoint Online: Site Users and Groups Report using PowerShell

Salaudeen Rajack

Information Technology Professional with Two decades of SharePoint Experience.

2 thoughts on “Get All Users and Groups in SharePoint Online Site using PowerShell-CSOM

  • September 30, 2019 at 4:04 PM

    How can you get the groups (and users within) from a subsite, modifying the code to call with the url to the subsite still reverts to getting groups from the parent level rather than the subsite?

    Reply
    • October 2, 2019 at 3:12 PM

      If your subsite is inheriting permissions from its parent, Then You’ll get parent permissions only!

      Reply

Leave a Reply