kwizcom banner advertisement

SharePoint Online: How to Create a Group using PowerShell?

A SharePoint group is a container of users who share similar set of permissions. SharePoint groups provides convenient way to manage security as a group centrally rather managing individual users. We can organize users into groups (up to 5,000) and set permissions to the group at site level and then use it in any underlying objects like sub-sites, lists and list items.

By default SharePoint creates three user groups upon site creation: Owners, Members and visitors and you need to be a site owner/site collection admin to grant permissions to the group.

How to Create a Group in SharePoint Online:
Here is how to create SharePoint Online group.
  • Navigate to your SharePoint online site, Click on Site Settings gear icon and then select Site Settings. From the Site settings page, click Site Permissions in the Users And Permissions section.
  • From the Permissions page, click on Create Group icon from the ribbon, under Permissions tab.
    SharePoint Online PowerShell to create group
  • On the Create Group page, provide the Name, optionally other settings such as description, owner to the group. Set group settings like who can view/edit group membership, Allow users to request membership,  leave the group. All requests will be sent to the specified E-mail.
  • In the "Give group permission to this site" section, select the appropriate check box to assign the permission level to your new group.
    SharePoint online Create Group using PowerShell
  • Click Create.
Once the group is created, you can rename it, change permissions of the group in one place and add/remove people to it. Lets see the PowerShell for SharePoint online to create group.

Create Group in SharePoint Online using PowerShell
The New-SPOSiteGroup PowerShell cmdlet lets you create a new SharePoint group in SharePoint Online site collection. This cmdlet requires the site collection's URL where you wish to create this new SharePoint group, name of the new group, and the permission level(s) that you wish to assign to it. The below PowerShell script shows how you can use this cmdlet to create a new security group "Marketing Staff" in your SharePoint Online site called Marketing with both Edit and Design rights.

Here is an example of creating a new user group in SharePoint Online using PowerShell
#Variables for Admin Center & Site Collection URL
$AdminCenterURL = "https://crescent-admin.sharepoint.com/"
$SiteCollectionURL = "https://crescent.sharepoint.com/sites/marketing"

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

#Create a new SharePoint Group
New-SPOSiteGroup -Site $SiteCollectionURL -Group "Marketing Staff" -PermissionLevels "Design", "Edit"
This cmdlet creates a new group in the SharePoint Online site collection.


SharePoint Online PowerShell to Create Group using CSOM
Lets create a new user group in SharePoint Online using PowerShell CSOM.
#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"
 
#Variables for Processing
$SiteURL = "https://Crescent.sharepoint.com/Sites/Sales"
$GroupName="Sales Managers90"

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

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

#Create new Group
$GroupInfo = New-Object Microsoft.SharePoint.Client.GroupCreationInformation
$GroupInfo.Title = $GroupName      
$Group = $Ctx.web.SiteGroups.Add($GroupInfo)
$Ctx.ExecuteQuery()
Plain and simple, huh.. Lets add some error handling code and assign permissions to the created group.

SharePoint Online Create Group using PowerShell
Here is the PowerShell CSOM script to create SharePoint 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"
 
#Variables for Processing
$SiteURL = "https://crescent.sharepoint.com/Sites/Sales"
$GroupName="Sales Managers"
$PermissionLevel="Edit"

#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 existing groups of the site
    $Groups = $Ctx.Web.SiteGroups
    $Ctx.load($Groups)
    $Ctx.ExecuteQuery()
    
    #Get Group Names
    $GroupNames =  $Groups | Select -ExpandProperty Title
    
    #Check if the given group doesn't exist already
    If($GroupNames -notcontains $GroupName)
    {
        #Create new Group
        $GroupInfo = New-Object Microsoft.SharePoint.Client.GroupCreationInformation
        $GroupInfo.Title = $GroupName      
        $Group = $Ctx.web.SiteGroups.Add($GroupInfo)
        $Ctx.ExecuteQuery()

        #Assign permission to the group
        $RoleDef = $Ctx.web.RoleDefinitions.GetByName($PermissionLevel)
        $RoleDefBind = New-Object Microsoft.SharePoint.Client.RoleDefinitionBindingCollection($Ctx)
        $RoleDefBind.Add($RoleDef)
        $Ctx.Load($Ctx.Web.RoleAssignments.Add($Group,$RoleDefBind))
        $Ctx.ExecuteQuery()

        write-host  -f Green "User Group has been added Successfully!"
    }
    else
    {
        Write-host -f Yellow "Group Exists already!"
    }
}
Catch {
    write-host -f Red "Error Creating New user Group!" $_.Exception.Message
}
This script creates new SharePoint Online group and assigns permission to the group.
SharePoint Online: How to Create a Group using PowerShell? SharePoint Online: How to Create a Group using PowerShell? Reviewed by Salaudeen Rajack on April 02, 2016 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.