SharePoint Online: Get All Users in Site Collection using PowerShell

Requirement: SharePoint Online PowerShell to Get All Users in Site Collection

While my another post addresses How to Get All Users and Groups in SharePoint Online, this post aims to get all users of the SharePoint site collection.

SharePoint Online: PowerShell Get All Users of the Site Collection
Here is the SharePoint Online CSOM to get all users
#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"

#Site collection URL
$SiteURL="https://crescent.sharepoint.com"

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

#Initialize the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials 

#Get all users of the site collection
$Users = $ctx.Web.SiteUsers
$ctx.Load($Users) 
$ctx.ExecuteQuery()

#Get User name and Email
$Users | ForEach-Object { Write-Host "$($_.Title) - $($_.Email)"}

PowerShell to Get All Users from All Site Collections in SharePoint Online:
Here is the SharePoint Online Management Shell way to get all users from all site collections in the tenant.
Import-Module Microsoft.Online.Sharepoint.PowerShell -DisableNameChecking

$AdminSiteURL="https://crescent-admin.sharepoint.com/"

#Connect to SharePoint Online Admin
Write-host "Connecting to Admin Center..." -f Yellow
Connect-SPOService -url $AdminSiteURL -Credential (Get-Credential)

Write-host "Getting All Site collections..." -f Yellow
#Get each site collection and users
$Sites = Get-SPOSite -Limit ALL

Foreach($Site in $Sites)
{
    Write-host "Getting Users from Site collection:"$Site.Url -f Yellow
    Get-SPOUser -Limit ALL -Site $Site.Url | Select DisplayName, LoginName
}
This PowerShell script gets all users in SharePoint Online.

PnP PowerShell to Get All Users in SharePoint Online Site Collection 
#Config Variables
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
  
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -UseWebLogin 
 
#Get All users of the site collection
Get-PnPUser 
This script gets you all users from the current site collection regardless if they have to access rights to the current site. You can add -WithRightsAssigned switch to get all users who have access to the site.
#Set Variables
$SiteURL = "https://crescent.sharepoint.com/sites/marketing/2017"
  
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -UseWebLogin 
$Web = Get-PnPWeb
 
#Get All users who have permission to the subsite 
Get-PnPUser -WithRightsAssigned -Web $Web 

SharePoint Online: Export All Users in a Site Collection to CSV using PnP PowerShell
#Config Variables
$SiteURL = "https://crescent.sharepoint.com/"
$CSVFile = "C:\Temp\UserData.csv"
  
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-credential)  #-UseWebLogin 
 
#Get All users of the site collection
$Users = Get-PnPUser
$UserData=@()

#Loop through Users and get properties
ForEach ($User in $Users)
{
    $UserData += New-Object PSObject -Property @{
        "User Name" = $User.Title
        "Login ID" = $User.LoginName
        "E-mail" = $User.Email
        "User Type" = $User.PrincipalType
    }
}
$UserData
#Export Users data to CSV file
$UserData | Export-Csv -NoTypeInformation $CSVFile
and the script output:
sharepoint online powershell get all users in site collection
SharePoint Online: Get All Users in Site Collection using PowerShell SharePoint Online: Get All Users in Site Collection using PowerShell Reviewed by Salaudeen Rajack on February 11, 2017 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.