Continuing the series of posts about how to work with permissions and permission levels in SharePoint Online using the client side object model, this time I am sharing a PowerShell script about how to get the list of all SharePoint groups in a SharePoint Online site using the Client Side Object Model instead of Get-SPOSiteGroup command.
1: ############################################################################################################################################
2: # Script that allows to get all the SharePoint groups in a SharePoint Online Site
3: # Required Parameters:
4: # -> $sUserName: User Name to connect to the SharePoint Online Site Collection.
5: # -> $sPassword: Password for the user.
6: # -> $sSiteCollectionUrl: SharePoint Online Site
7: ############################################################################################################################################
8:
9: $host.Runspace.ThreadOptions = "ReuseThread"
10:
11: #Definition of the function that gets all the SharePoint groups in a SharePoint Online site
12: function Get-SPOSharePointGroupsInSite
13: {
14: param ($sSiteColUrl,$sUsername,$sPassword)
15: try
16: {
17: Write-Host "----------------------------------------------------------------------------" -foregroundcolor Green
18: Write-Host "Getting all Groups in a SharePoint Online Site" -foregroundcolor Green
19: Write-Host "----------------------------------------------------------------------------" -foregroundcolor Green
20:
21: #Adding the Client OM Assemblies
22: Add-Type -Path "<CSOM_Path>\Microsoft.SharePoint.Client.dll"
23: Add-Type -Path "<CSOM_Path>\Microsoft.SharePoint.Client.Runtime.dll"
24:
25: #SPO Client Object Model Context
26: $spoCtx = New-Object Microsoft.SharePoint.Client.ClientContext($sSiteColUrl)
27: $spoCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($sUsername, $sPassword)
28: $spoCtx.Credentials = $spoCredentials
29:
30: #Root Web Site
31: $spoRootWebSite = $spoCtx.Web
32: #Collecction of Sites under the Root Web Site
33: $spoSites = $spoRootWebSite.Webs
34:
35: #Loading Operations
36: $spoGroups=$spoCtx.Web.SiteGroups
37: $spoCtx.Load($spoGroups)
38: $spoCtx.ExecuteQuery()
39:
40: #We need to iterate through the $spoGroups Object in order to get individual Group information
41: foreach($spoGroup in $spoGroups){
42: $spoCtx.Load($spoGroup)
43: $spoCtx.ExecuteQuery()
44: Write-Host $spoGroup.Title
45: }
46:
47: $spoCtx.Dispose()
48: }
49: catch [System.Exception]
50: {
51: write-host -f red $_.Exception.ToString()
52: }
53: }
54:
55: #Required Parameters
56: $sSiteColUrl = "https://<SPO_SiteUrl>"
57: $sUsername = "<SPOUserName>"
58: #$sPassword = Read-Host -Prompt "Enter your password: " -AsSecureString
59: $sPassword=convertto-securestring "<SPO_Password>" -asplaintext -force
60:
61: Get-SPOSharePointGroupsInSite -sSiteColUrl $sSiteColUrl -sUsername $sUsername -sPassword $sPassword
You can download the PowerShell script from the following download page in the Script Center Gallery in TechNet: How to get all the SharePoint Groups in a SharePoint Online Site. Finally, bellow you can see the screenshot you get when you execute the script in the SharePoint Online Management Shell:
References: