Tuesday, July 30, 2019

Get all the users per SharePoint Group in a SharePoint Online Site

$host.Runspace.ThreadOptions = "ReuseThread"

#Definition of the function that gets all the site collections information in a SharePoint Online tenant
function Get-SPOSharePointUsersPerGroup
{
    param ($sSPOAdminCenterUrl,$sSiteUrl,$sUserName,$sPassword)
    try
    {   
        Write-Host "--------------------------------------------------------------------------------"  -foregroundcolor Green
        Write-Host "Getting all Users per Group in a SharePoint Online Site" -foregroundcolor Green
        Write-Host "--------------------------------------------------------------------------------"  -foregroundcolor Green   
        $msolcred = Get-Credential -UserName $sUserName -Message $sMessage
        Connect-SPOService -Url $sSPOAdminCenterUrl -Credential $msolcred
        $spoGroups=Get-SPOSiteGroup -Site $sSiteUrl

        foreach($spoGroup in $spoGroups){       
           Write-Host "Users in " $spoGroup.Title ":"
           $spoUsers=Get-SPOUser -Site $sSiteUrl -Group $spoGroup.Title
           Write-Host " -> " $spoUsers.LoginName
           Write-Host "---------------------------------------------------" -ForegroundColor Green
        }
    }
    catch [System.Exception]
    {
        write-host -f red $_.Exception.ToString() 
    }   
}

#Required Parameters
$sSiteUrl = "https://<SharePointOnline_SiteUrl"
$sUserName = "<SharePointOnlineUser>"
$sMessage="<Custom_Prompt_Message>"
$sSPOAdminCenterUrl="https://<SPODomain>-admin.sharepoint.com/"

Get-SPOSharePointUsersPerGroup -sSPOAdminCenterUrl $sSPOAdminCenterUrl -sSiteUrl $sSiteUrl -sUserName $sUsername -sPassword $sPassword

Get All Users and Groups using Powershell in Sharepoint Online Site

#Load SharePoint CSOM Assemblies
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"
 
#Set Variables for Site URL
$SiteURL= "https://SharepointSiteCollectionURL/"

#Setup Credentials to connect
$Cred = Get-Credential
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)

Try {
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Cred

    #Get all Groups
    $Groups=$Ctx.Web.SiteGroups
    $Ctx.Load($Groups)
    $Ctx.ExecuteQuery()

    #Get Each member from the Group
    Foreach($Group in $Groups)
    {
        Write-Host "--- $($Group.Title) --- "

        #Getting the members
        $SiteUsers=$Group.Users
        $Ctx.Load($SiteUsers)
        $Ctx.ExecuteQuery()
        Foreach($User in $SiteUsers)
        {
            Write-Host "$($User.Title), $($User.Email), $($User.LoginName)"
        }
    }
}
Catch {
    write-host -f Red "Error getting groups and users!" $_.Exception.Message
}