top of page

Retrieve/Add SharePoint Site Groups And Users Using CSOM PowerShell


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  

  2. #To get root site details 

  3. $web = $ctx.Site.OpenWeb("/SharePoint")  

  4. $groups = $web.SiteGroups # Gets all site groups  

  5. $siteUsers = $web.SiteUsers # Gets all users who have access to site in any way  

  6. $ctx.Load($groups)  

  7. $ctx.Load($siteUsers)  

  8. $ctx.ExecuteQuery()  

6. Get the site group details and group member details who are all part of each group.

  1. # Gets all the groups foreach($group in $groups){      

  2. Write-Host "Site Group : " $group.Title      

  3. Write-Host $group.Description      

  4. $users = $group.Users      

  5. $ctx.Load($users)      

  6. $ctx.ExecuteQuery()      

  7. Write-Host "Group Members:- "     

  8. foreach($user in $users){          

  9. Write-Host " " $user.Title      }      

  10. Write-Host "----------------------"

  11. }  

7. Get site users.

  1. # This will display all the users who have access to the site foreach($siteUser in $siteUsers)

  2. {      

  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

$newGroupInfo = New-Object Microsoft.SharePoint.Client.GroupCreationInformation  

$newGroupInfo.Title = "CustomSPGroup"

$newGroupInfo.Description = "Custom Group"

$newGroup = $web.SiteGroups.Add($newGroupInfo)  



3. Then add new user to the above group. The following snippets depicts the flow. $userInfo = $web.EnsureUser("")  


$addUser = $newGroup.Users.AddUser($userInfo)  



4. Add the SharePoint group to the site with necessary information.

  • Break the root level inheritance permission.

  • Get the role definition with permission details and add it to site with the group details.

  • Load, update and execute the query.

$access = $web.RoleDefinitions.GetByName("Read")  

$roleAssignment =  New- Object Microsoft.SharePoint.Client.RoleDefinitionBindingCollection($ctx)  


$addPermission = $web.RoleAssignments.Add($newGroup, $roleAssignment)  

$web.BreakRoleInheritance($false, $false)  





The new group has been created along with the members. It has been added to the site with read level permissions.


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.



bottom of page