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.
- 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.
- Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
- Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
- Initialize client context object with the site URL.
- $siteURL = ""
- $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)
- 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.
- #Not required for on premise site - Start
- $userId = ""
- $pwd = Read-Host -Prompt "Enter password" -AsSecureString
- $creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userId, $pwd)
- $ctx.credentials = $creds
- #Not required for on premise site - End
- 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.
- 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.
- #$web = $ctx.Site.RootWeb #To get root site details
- $web = $ctx.Site.OpenWeb("/SharePoint")
- $groups = $web.SiteGroups # Gets all site groups
- $siteUsers = $web.SiteUsers # Gets all users who have access to site in any way
- $ctx.Load($groups)
- $ctx.Load($siteUsers)
- $ctx.ExecuteQuery()
- Get the site group details and group member details who are all part of each group.
- # Gets all the groups
- foreach($group in $groups){
- Write-Host "Site Group : " $group.Title
- Write-Host $group.Description
- $users = $group.Users
- $ctx.Load($users)
- $ctx.ExecuteQuery()
- Write-Host "Group Members:- "
- foreach($user in $users){
- Write-Host " " $user.Title
- }
- Write-Host "----------------------"
- }
- Get site users.
- #This will display all the users who have access to the site
- foreach($siteUser in $siteUsers){
- Write-Host $siteUser.Title
- }

Humayun Kabir MamunPosted May 22, 2016, 7:36 AM
Nice...
Vignesh ManiPosted May 21, 2016, 4:32 PM
good
Sonu ChaudharyPosted May 19, 2016, 11:34 AM
good one
Debasis SahaPosted May 19, 2016, 6:31 AM
Good One..
Bhushan GawalePosted May 19, 2016, 2:24 AM
Informative!
Kuppurasu NagarajPosted May 18, 2016, 1:45 PM
Nice Sharing..
Neeraj KumarPosted May 18, 2016, 12:59 PM
Nice one
Debasis SahaPosted May 18, 2016, 10:21 AM
Good One..
Ketak BhalsingPosted May 18, 2016, 8:18 AM
Very Well Written.....