Get All Users and Groups in SharePoint Online Site using PowerShell-CSOM

Requirement: Get All Users and Groups Report in SharePoint Online using PowerShell-CSOM
Get All Users and Groups in SharePoint Online Site using PowerShell

PowerShell Script to Get All Users and Groups Report in SharePoint Online:
This PowerShell script gets all groups and users of each group in a SharePoint online site.
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
  
#Set Variables for Site URL
$SiteURL= "https://crescent.sharepoint.com/sites/sales/"

#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
}
Here is my another article to get all groups and group members from SharePoint online using SharePoint online shell: SharePoint Online: Site Users and Groups Report using PowerShell
Get All Users and Groups in SharePoint Online Site using PowerShell-CSOM Get All Users and Groups in SharePoint Online Site using PowerShell-CSOM Reviewed by Salaudeen Rajack on October 17, 2016 Rating: 5

2 comments:

  1. How can you get the groups (and users within) from a subsite, modifying the code to call with the url to the subsite still reverts to getting groups from the parent level rather than the subsite?

    ReplyDelete
    Replies
    1. If your subsite is inheriting permissions from its parent, Then You'll get parent permissions only!

      Delete

Please Login and comment to get your questions answered!

Powered by Blogger.