Get All Users and Groups in SharePoint Online Site using PowerShell

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

Get All Users and Groups in SharePoint Online Site using PowerShell

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

Do you need to get a list of all users and groups in SharePoint Online? If so, PowerShell can help! Exporting users and groups from a SharePoint Online site can be done with PowerShell. Let’s find out how to easily produce an overview report for the site users and groups in Excel format.

This PowerShell CSOM 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
}

This can be helpful if you need to get a list of all the users or groups in a site for reporting purposes, troubleshoot access issues, or if you need to remove a user or group from the site.

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

If you need to export SharePoint online users and groups to excel, PnP PowerShell cmdlets can help. The Get-PnPGroup cmdlet provides a way to export SharePoint online Groups and users membership in each group. To use this cmdlet, you will need to connect to SharePoint online using the Connect-PnPOnline cmdlet. Once you are connected, you can use the Get-PnPGroup cmdlet to export the desired user data to a CSV file. This file can then be opened in Excel for further analysis.

Let’s export all users and users from a site into a CSV file:

#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)  #-Interactive

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

#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 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

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

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

  • 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
    • If your subsite is inheriting permissions from its parent, Then You’ll get parent permissions only!

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *