Retrieve/Add SharePoint Site Groups And Users Using CSOM PowerShell

Introduction

In this article, you will learn how to get the Site groups and users from the SharePoint site or how to create and add site groups with members using CSOM with PowerShell on SharePoint 2013 / SharePoint online.

Get groups and users

The following section explains in detail about getting the groups and members of SharePoint site.

  1. Add the references using the Add-Type command with necessary reference paths. The necessary references are Microsoft.SharePoint.Client.dll, Microsoft.SharePoint.Client.Runtime.dll and Microsoft.SharePoint.Client.Publishing.dll.
    1. Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"  
    2. Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"  

  2. Initialize client context object with the site URL.
    1. $siteURL = ""  
    2. $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)  

  3. If you are trying to access SharePoint Online site, then you need to setup the site credentials with credentials parameter and get it set to the client context. 

    1. #Not required for on premise site - Start  
    2. $userId = ""  
    3. $pwd = Read-Host -Prompt "Enter password" -AsSecureString  
    4. $creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userId, $pwd)  
    5. $ctx.credentials = $creds   
    6. #Not required for on premise site - End  

  4. If you are trying to access the SharePoint on premise site, then the credentials parameter is not required to be set to the context. But you need to run the code on the respective SharePoint server.

  5. Get the site groups and site users by loading and executing the query. Here you can retrieve your own custom sub site. Groups will fetch all the groups. SiteUsers method will fetch us all the users who have access to the site.
    1. #$web = $ctx.Site.RootWeb  #To get root site details   
    2. $web = $ctx.Site.OpenWeb("/SharePoint")  
    3. $groups = $web.SiteGroups # Gets all site groups  
    4. $siteUsers = $web.SiteUsers # Gets all users who have access to site in any way  
    5. $ctx.Load($groups)  
    6. $ctx.Load($siteUsers)  
    7. $ctx.ExecuteQuery()  

  6. Get the site group details and group member details who are all part of each group.
    1. # Gets all the groups  
    2. foreach($group in $groups){  
    3.     Write-Host "Site Group : " $group.Title  
    4.     Write-Host $group.Description  
    5.     $users = $group.Users  
    6.     $ctx.Load($users)  
    7.     $ctx.ExecuteQuery()  
    8.     Write-Host "Group Members:- "  
    9.     foreach($user in $users){  
    10.         Write-Host " " $user.Title  
    11.     }  
    12.     Write-Host "----------------------"  
    13. }  

  7. Get site users.
    1. #This will display all the users who have access to the site  
    2. foreach($siteUser in $siteUsers){  
    3.     Write-Host $siteUser.Title          
    4. }  

Create and Add groups with users

The following section explains in detail about creating and adding the groups with members to SharePoint site.

  1. Execute first 5 steps from the above section.

  2. Create new group.

    • Then create new group with Group creation information object with title and descriptions.
    • Add it to the existing site group collection
    • Load and execute the query
    1. $newGroupInfo = New-Object Microsoft.SharePoint.Client.GroupCreationInformation  
    2. $newGroupInfo.Title = "CustomSPGroup"  
    3. $newGroupInfo.Description = "Custom Group"  
    4.   
    5. $newGroup = $web.SiteGroups.Add($newGroupInfo)  
    6. $ctx.Load($newGroup)  
    7. $ctx.ExecuteQuery()  
  3. Then add new user to the above group. The following snippets depicts the flow.
    1. $userInfo = $web.EnsureUser("[email protected]")  
    2. $ctx.Load($userInfo)  
    3. $addUser = $newGroup.Users.AddUser($userInfo)  
    4. $ctx.Load($addUser)  
    5. $ctx.ExecuteQuery()  
  4. Add the SharePoint group to the site with necessary information.

    1. Break the root level inheritance permission.
    2. Get the role definition with permission details and add it to site with the group details.
    3. Load, update and execute the query.
      1. $access = $web.RoleDefinitions.GetByName("Read")  
      2. $roleAssignment =  New-Object Microsoft.SharePoint.Client.RoleDefinitionBindingCollection($ctx)  
      3. $roleAssignment.Add($access)  
      4. $addPermission = $web.RoleAssignments.Add($newGroup, $roleAssignment)  
      5. $web.BreakRoleInheritance($false, $false)  
      6. $ctx.Load($web)  
      7. $ctx.Load($addPermission)  
      8. $web.Update()  
      9. $ctx.ExecuteQuery()  
The new group has been created along with the members. It has been added to the site with read level permissions.
 
Summary
 
Thus you have learned how to retrieve the site users and site groups from the site and how to add groups with users to the site using CSOM with PowerShell commands on SharePoint 2013 / SharePoint online.

Up Next
    Ebook Download
    View all
    Learn
    View all