Microsoft 365 : SharePoint Online – PowerShell script – PowerShell script to create SharePoint group in a given site collection

PowerShell script for creating SharePoint group in SharePoint site collection
PowerShell script for creating SharePoint group in SharePoint site collection

Hi All,

Greetings for the day 🙂 LIFE IS BEAUTIFUL 🙂

Today one more PowerShell script for SharePoint online in our PowerShell kitty 🙂 Small but very useful so SHARING IS CARING 🙂

Details / Background :

  • In one of our project we are inserting one record in a list
  • As on insert record in a list, we were creating one SharePoint group, adding respective users to the group, breaking the inheritance on item and giving permission to the group
  • While doing some research I found that we have group limit per site collection is only 10,000
  • SharePoint site group limitations up to 10000. Reference URL – SharePoint limits – https://docs.microsoft.com/en-us/office365/servicedescriptions/sharepoint-online-service-description/sharepoint-online-limits  (You can have up to 10,000 groups per site (site collection).)
  • Actually this limitation is applicable for both the environments – On-Premises and Online.
  • So this means if our list have 10,000 items, we have 10,000 groups and this may stop the group creation or the whole process will be very very slow
  • As per MS documentation –
SharePoint groups10,000 per site collectionSupportedAbove 10,000 groups, the time to execute operations is increased significantly. This implication is especially true of adding a user to an existing group, creating a new group, and rendering group views.

So to verify this thought to really create the 10,000 groups in one of our test site. We could achieve this using PowerShell CMDLET.

Step by Step details for PowerShell CMDLET – for creating 10,000 groups in a given site collection :

  • Open the Windows PowerShell ISE as
Windows PowerShell ISE
fig : Windows PowerShell ISE
  • Connect to SharePoint Online using SharePoint admin site URL and Connect-SPOService CMDLET as
#Variable for Admin Center URL
$AdminCenterURL = "https://knowledgejunction1-admin.sharepoint.com/"

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

  • Here, we are getting credentials , when we execute above CMDLET, we will get an dialog to enter the credentials as
Microsoft 365 - SharePoint Online - executing command Connect-SPOService - Getting credentials
fig : Microsoft 365 – SharePoint Online – executing command Connect-SPOService – Getting credentials
  • Once we are successfully connected to SharePoint Online we are ready to create the 10,000 groups on the given site collection
  • For group creation we will use – New-SPOSiteGroup – CMDLET as
New-SPOSiteGroup -Site <$SiteCollectionURL> -Group <$groupdisplayname> -PermissionLevels <"Edit">

New-SPOSiteGroup
   -Group <String>
   -PermissionLevels <String[]>
   -Site <SpoSitePipeBind>
   [<CommonParameters>]

  • New-SPOSiteGroup CMDLET requires following parameters
    • Site : Site collection URL where we need to create the Group
    • Group : Group display name
    • PermissionLevels : Which permissions we need to assign to the Group.

Example : Following sample code adds the group called “KnowledgeJunctionDemoGroup” to the site collection – https://knowledgejunction1.sharepoint.com/ with “Edit” permissions

#Variable for Site Collection URL

$SiteCollectionURL = "https://knowledgejunction1.sharepoint.com/"
$groupdisplayname = "KnowledgeJunctionDemoGroup"

#sharepoint online powershell add group to site

New-SPOSiteGroup -Site $SiteCollectionURL -Group $groupdisplayname -PermissionLevels "Edit"

  • Since we need to create 10,000 groups, we need to apply the For-Loop as
for ($groupCounter=0; $groupCounter -lt 10000; $groupCounter++){
    $groupdisplayname = "Group" + $groupCounter
    #sharepoint online powershell add group to site
    New-SPOSiteGroup -Site $SiteCollectionURL -Group $groupdisplayname -PermissionLevels "Edit"
} #for ($groupCounter=0; $groupCounter -lt 10000; $groupCounter++)

Permissions require to execute this script : User who runs this script must be either SharePoint Online Administrator OR Global Administrator

Complete script :

#Variables for Admin Center & Site Collection URL

$AdminCenterURL = "https://knowledgejunction1-admin.sharepoint.com/"
$SiteCollectionURL = "https://knowledgejunction1.sharepoint.com/"
 
#Connect to SharePoint Online
Connect-SPOService -url $AdminCenterURL -Credential (Get-Credential)
 

for ($groupCounter=0; $groupCounter -lt 10000; $groupCounter++){
    $groupdisplayname = "Group" + $groupCounter
    #sharepoint online powershell add group to site
    New-SPOSiteGroup -Site $SiteCollectionURL -Group $groupdisplayname  -PermissionLevels "Edit"

} #for ($groupCounter=0; $groupCounter -lt 10000; $groupCounter++)

Executing the script for creating SharePoint group
fig : Executing the script for creating SharePoint group

We have an option of creating SharePoint Group using PnP PowerShell as well, which we will discuss in upcoming articles 🙂

Thanks for reading 🙂 HAVE A GREAT TIME AHEAD 🙂

Prasham Sabadra

LIFE IS VERY BEAUTIFUL :) ENJOY THE WHOLE JOURNEY :) Founder of Knowledge Junction and live-beautiful-life.com, Author, Learner, Passionate Techie, avid reader. Certified Professional Workshop Facilitator / Public Speaker. Scrum Foundation Professional certificated. Motivational, Behavioral , Technical speaker. Speaks in various events including SharePoint Saturdays, Boot camps, Collages / Schools, local chapter. Can reach me for Microsoft 365, Azure, DevOps, SharePoint, Teams, Power Platform, JavaScript.

You may also like...

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: