I would like to know how can I adjust the script (link below) in order to get all the members inside each group as well?

Many thanks.

Get group owners - Sharepoint Online

1 Answer 1


here is the script you can run to get members of the group.

#Add - PSSnapin Microsoft.SharePoint.PowerShell

$host.Runspace.ThreadOptions = "ReuseThread" 

Function Get-SiteGroup-Owners 
    param ($sCSOMPath,$sSiteUrl,$sUserName,$sPassword,$FileName) 
        $sCSOMRuntimePath=$sCSOMPath +  "\Microsoft.SharePoint.Client.Runtime.dll"         
        $sCSOMPathdll=$sCSOMPath +  "\Microsoft.SharePoint.Client.dll"              
        Add-Type -Path $sCSOMPathdll          
        Add-Type -Path $sCSOMRuntimePath        

        #SPO Client Object Model Context 
        $spoCtx = New-Object Microsoft.SharePoint.Client.ClientContext($sSiteUrl) 
        $spoCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($sUserName, $sPassword)   
        #$spoCredentials = New-Object System.Net.NetworkCredential($sUserName, $sPassword)    # for on premise
        $spoCtx.Credentials = $spoCredentials        

        $web = $spoCtx.Web
        $spGroups =$web.SiteGroups


        Write-Host "Groups Count= $($spGroups.Count)"

        foreach($spGroup in $spGroups)
            $site =@()
            $users = $spGroup.Users

            foreach($spUser in $users)

                    $site += New-Object -TypeName PSObject -Property @{
                                    SiteUrl =$sSiteUrl
                                    GroupName = $spGroup.Title
                                    PrincipalType= $spGroup.PrincipalType
                                    User =$spUser.LoginName
                                    UserEmail =$spUser.Email
                                    } | Select  SiteUrl,GroupName,PrincipalType,User,UserEmail

                catch [System.Exception] 
                    Write-Host -ForegroundColor Red $_.Exception.ToString()    


            $site| Export-CSV ($sCSOMPath+"\"+$FileName+".csv") -NoTypeInformation -Append #-Encoding UTF8          


        Read-Host -Prompt "file created Successfull..! in the following path $sCSOMPath, Press any key to close this window"

    catch [System.Exception] 
        Write-Host -ForegroundColor Red $_.Exception.ToString()    
        Read-Host -Prompt "Operation failed..! Press any key to close this and re run the script"

$FileName= "SiteCollection_Group_Members"          
$sSiteUrl = "Site Collection Url"  
$sUserName = "Enter User" 
$sPassword = ConvertTo-SecureString "Password" -AsPlainText -Force 

$scriptpath = $MyInvocation.MyCommand.Path
$dir = Split-Path $scriptpath

Write-Host "Getting Site Collection group Members"
Write-Host "-----------------------------------" 
Get-SiteGroup-Owners -sCSOMPath $dir -sSiteUrl $sSiteUrl -sUserName $sUserName -sPassword $sPassword  -FileName $FileName
  • how to generate an excel file out of this solution? is it possible to get all the sp groups and export it to an excel or csv file ?
    – samolpp2
    Commented May 31, 2017 at 9:54
  • 1
    csv file is generating from the above script. yes you can get all sp groups in a site collection. In the above script remove user for loop and keep this $site +=New-Object -TypeName PSObject -Property @{ SiteUrl =$sSiteUrl GroupName = $spGroup.Title PrincipalType= $spGroup.PrincipalType } | Select SiteUrl,GroupName,PrincipalType keep it in groups for loop Commented May 31, 2017 at 11:30
  • mine is a sp on-premises farm. not spo. so should i actually pass username , password params into your function?
    – samolpp2
    Commented May 31, 2017 at 13:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.