This script gets for each SharePoint Group in a SharePoint site all the users contained on it. The script defines a function that uses the client side object model (CSOM) as the foundations to get all the users per SharePoint group in a SharePoint Online site. The function uses standard SPO objects to get all the users in all the SharePoint Groups in a SharePoint Online site.

 

PowerShell
Editar script|Remove
############################################################################################################################################ 
#Script that allows to get all the users per SharePoint Group in a SharePoint Online Site 
# Required Parameters: 
#  -> $sUserName: User Name to connect to the SharePoint Online Site Collection. 
#  -> $sPassword: Password for the user. 
#  -> $sSiteCollectionUrl: SharePoint Online Site 
############################################################################################################################################ 
 
$host.Runspace.ThreadOptions = "ReuseThread" 
 
#Definition of the function that gets all the SharePoint groups in a SharePoint Online site 
function Get-SPOAllSharePointUsersInGroups 
{ 
    param ($sSiteColUrl,$sUsername,$sPassword) 
    try 
    {     
        Write-Host "----------------------------------------------------------------------------"  -foregroundcolor Green 
        Write-Host "Getting all Groups in a SharePoint Online Site" -foregroundcolor Green 
        Write-Host "----------------------------------------------------------------------------"  -foregroundcolor Green 
      
        #Adding the Client OM Assemblies         
        Add-Type -Path "<CSOM_Path>\Microsoft.SharePoint.Client.dll" 
        Add-Type -Path "<CSOM_Path>\Microsoft.SharePoint.Client.Runtime.dll" 
 
        #SPO Client Object Model Context 
        $spoCtx = New-Object Microsoft.SharePoint.Client.ClientContext($sSiteColUrl)  
        $spoCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($sUsername$sPassword)   
        $spoCtx.Credentials = $spoCredentials  
 
        #Root Web Site 
        $spoRootWebSite = $spoCtx.Web 
        #Collecction of Sites under the Root Web Site 
        $spoSites = $spoRootWebSite.Webs 
 
        #Loading Operations 
        $spoGroups=$spoCtx.Web.SiteGroups 
        $spoCtx.Load($spoGroups) 
        $spoCtx.ExecuteQuery() 
         
        #We need to iterate through the $spoGroups Object in order to get individual Group information 
        foreach($spoGroup in $spoGroups){ 
            $spoCtx.Load($spoGroup) 
            $spoCtx.ExecuteQuery() 
            Write-Host "* " $spoGroup.Title 
 
            #Getting the users per group in the SPO Site 
            $spoSiteUsers=$spoGroup.Users 
            $spoCtx.Load($spoSiteUsers) 
            $spoCtx.ExecuteQuery() 
            foreach($spoUser in $spoSiteUsers){ 
                Write-Host "    -> " $spoUser.Title " - " $spoUser.Id " - " $spoUser.Email " - " $spoUser.LoginName                 
            } 
        } 
 
        $spoCtx.Dispose() 
    } 
    catch [System.Exception] 
    { 
        write-host -f red $_.Exception.ToString()    
    }     
} 
 
#Required Parameters 
$sSiteColUrl = "https://<Your_SPO_Site>"  
$sUsername = "<Your_SPO_User>"  
#$sPassword = Read-Host -Prompt "Enter your password: " -AsSecureString   
$sPassword=convertto-securestring "<Your_SPO_Password>" -asplaintext -force 
 
Get-SPOAllSharePointUsersInGroups -sSiteColUrl $sSiteColUrl -sUsername $sUsername -sPassword $sPassword