SharePoint Online: PowerShell to Get All SharePoint Groups

Requirement: SharePoint Online PowerShell to List All Groups in a Site Collection.

Managing permissions and security groups is crucial for governing access to SharePoint sites and content. SharePoint allows the creation of security groups and Office 365 groups to assign users access levels to different sites, lists, libraries, or items. As owners of SharePoint environments, administrators need visibility into these SharePoint groups across their sites and tenants. Getting a consolidated view of all groups helps manage permissions better, analyze how they are utilized, identify unused groups to clean up, audit group owners and members, etc.

However, the SharePoint user interface does not provide a direct way to extract a report of all groups created inside a SharePoint site collection or the tenant. Navigating through each site to compile groups is also not feasible.

This is where leveraging PowerShell comes in handy. SharePoint Online Management Shell and PnP PowerShell provide powerful cmdlets to programmatically extract all SharePoint security groups. The output can be exported to monitor groups across the SharePoint estate.

In this article, I will cover the PowerShell commands and techniques you can utilize to get a list of all SharePoint security groups from a site collection.

How to Get All Groups in SharePoint?

As a site collection Administrator or site owner, you can view all users granted access to your site and all SharePoint groups associated with your site. To get SharePoint Groups using the web user interface in a SharePoint Online site, follow these steps:

  1. Browse to your SharePoint Online site in a web browser.
  2. Click on the settings gear icon (⚙️) in the top-right corner to open the settings menu. Select Site permissions from the drop-down menu.
    Get-SPOGroup
  3. Click on “Advanced Permission Settings” to access the People and Groups section in the site settings. Here, SharePoint displays all the groups that have been granted permission to access the site.
    get groups in sharepoint site

Alternatively, you can navigate to the Groups page within the SharePoint site using the following URL format: https://Your-Domain.sharepoint.com/sites/Your-Site/_layouts/15/groups.aspx. Replace Your-Domain and Your-Site with the actual URL of your SharePoint site.

Get SharePoint Online Groups using PowerShell: Get-SPOSiteGroup

The Get-SPOSiteGroup PowerShell cmdlet displays a list of all existing SharePoint groups for a specific site collection. Use the SharePoint Online Management Shell console to get user groups in SharePoint Online. It takes the following syntax:

Get-SPOSiteGroup -Site <Site-Collection-URL> [-Limit <Int32>]

This PowerShell cmdlet gets you all SharePoint groups, permission levels, and users in a site collection. You can get the permission level(s) associated with each group in the site collection by accessing the Roles property of a group. The limit parameter’s default value is 200.

SharePoint Online PowerShell to List All Groups

Here is a PowerShell example to get all groups from a SharePoint Online site:

#Admin Center URL
$AdminCenterURL = "https://Crescent-admin.sharepoint.com"

#Connect to SharePoint Online
Connect-SPOService -url $AdminCenterURL -Credential (Get-Credential)

#Get All Groups of a site collection
Get-SPOSiteGroup -Site "https://Crescent.sharepoint.com/sites/marketing"

Let’s get all groups, group permissions, and group users from a site:

#Get Group Name, Role and Users
Get-SPOSiteGroup -site https://Crescent.sharepoint.com/sites/marketing | Select Title, Roles, Users
sharepoint online powershell list all groups

How about getting all groups from all sites in the tenant?

Get All Groups from All Sites in the SharePoint Online Tenant

Here is the PowerShell script to get all groups from all SharePoint Online sites.

#Connect to SharePoint Admin Center
Connect-SPOService -Url https://crescent-admin.sharepoint.com

#Get list of sites and groups
Get-SPOSite | ForEach {
    Try {
        Write-host -f Yellow "Getting groups from site:" $_.Url
        Get-SPOSiteGroup -Site $_.Url | Format-Table -ErrorAction SilentlyContinue
    }
    catch {
         Write-host -f red "Encountered Error:"$_.Exception.Message
    }
}

SharePoint Online: PowerShell to Get Group

Use the Get-SPOSiteGroup cmdlet with the Group parameter to get a group in the SharePoint Online site collection.

Get-SPOSiteGroup -Site "https://Crescent.sharepoint.com/sites/marketing" -Group "Marketing Owners"

Export SharePoint Online Groups Data to CSV:

Let’s get all groups from a Microsoft SharePoint Online site and export the group’s data to a CSV file.

#Admin Center & Site collection URL
$AdminCenterURL = "https://Crescent-admin.sharepoint.com/"
$SiteURL = "https://Crescent.sharepoint.com/sites/marketing"
$CSVPath = "C:\Temp\Groups.csv"

#Connect to SharePoint Online
Connect-SPOService -url $AdminCenterURL

#Get All Groups of a site collection
$Groups = Get-SPOSiteGroup -Site $SiteURL
Write-host "Total Number of Groups Found:"$Groups.Count

$GroupsData = @()
ForEach($Group in $Groups)
{
    $GroupsData += New-Object PSObject -Property @{
        'Group Name' = $Group.Title
        'Permissions' = $Group.Roles -join ","
        'Users' =  $Group.Users -join ","
    }
}
#Export the data to CSV
$GroupsData | Export-Csv $CSVPath -NoTypeInformation 

Executing the above Windows PowerShell or PowerShell ISE produces a CSV file with all groups, their permission levels, and the users list.

SharePoint Online PnP PowerShell to Get Groups

You can get all groups from a SharePoint Online site with the cmdlet Get-PnPGroup. Make sure you have PnP.PowerShell module installed. Here is the PnP PowerShell to list SharePoint Online groups from the given site:

#Site collection URL
$SiteURL= "https://crescent.sharepoint.com/sites/marketing"

#Connect to SharePoint Online
Connect-PnPOnline -Url $SiteURL -Interactive

#Get All Groups from the site - Exclude Hidden, Limited Access and SharingLinks Groups
Get-PnPGroup | Where {$_.IsHiddenInUI -eq $false -and $_.LoginName -notlike "Limited Access*" -and $_.LoginName -notlike "SharingLinks*"}

You can also use the Get-PnPSiteGroup cmdlet that offers some additional group data!

#Site collection URL
$SiteURL= "https://Crescent.sharepoint.com/sites/marketing"
$CSVPath = "C:\Temp\GroupData.csv"
 
#Connect to SharePoint Online
Connect-PnPOnline -Url $SiteURL -Interactive

#Get All Groups from the site - Exclude Hidden, Limited Access and SharingLinks Groups
$Groups = Get-PnPSiteGroup | Where { $_.LoginName -notlike "Limited Access*" -and $_.LoginName -notlike "SharingLinks*"}

$GroupsData = @()
ForEach($Group in $Groups)
{
    #Collect data of the group
    $GroupsData += New-Object PSObject -Property @{
        'Group Name' = $Group.Title
        'Permissions' = $Group.Roles -join ","
        'Users' =  $Group.Users -join ","
    }
}
#Export the data to CSV
$GroupsData | Export-Csv $CSVPath -NoTypeInformation 

Conclusion

Managing groups is critical for governing access and security in SharePoint sites or portals. As a SharePoint administrator, you need visibility into the various groups created across different sites. This allows you to analyze permissions assigned, identify obsolete groups, and streamline access control. As we discussed in this article, PowerShell provides a programmatic way to extract the full list of SharePoint groups on a site. The key commands are Get-SPOSiteGroup and Get-PnPSiteGroup, which can retrieve all SharePoint groups.

Here is another post to get SharePoint Online groups from all site collections: 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!

4 thoughts on “SharePoint Online: PowerShell to Get All SharePoint Groups

  • my organization having 1000 subsites i want see selected subsites groups
    1 generate report list of groups available in subsites
    2 create groups in subsites
    3. delete groups in subsite

    Reply
  • Does $Group.Roles return value ?

    Reply
  • thanks – great script – question though, when i get the results via csv, its not displaying this data,. if out put to a txt file, it does show the data, but not in a very readable format.. Any ideas?

    Reply

Leave a Reply

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