SharePoint Online: Get All Users in Site Collection using PowerShell

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

SharePoint Online: PowerShell Get All Users from a Site Collection

As an admin of your SharePoint Online site, you may be required to get a list of all users who have access to the site. In this blog post, we will take a look at how to get all users in a SharePoint Online site collection using PowerShell. This can be handy if you need to get a list of all users for automation purposes. Suppose you need to generate a report to audit user permissions or get an overview of who is currently using the site collection. Let’s get started!

How to Get All Users from a SharePoint Online site?

The easiest way to get a list of all the users in a SharePoint Online site collection is through the Site Settings with this trick:

  1. Navigate to the top-level SharePoint site collection. (E.g., https://YourDomain.SharePoint.com/sites/Retail)
  2. Append “/_layouts/15/people.aspx?MembershipGroupId=0” to the URL to get the “All People” page. E.g., https://crescent.sharepoint.com/sites/Retail/_layouts/15/people.aspx?MembershipGroupId=0.

SharePoint will return all users who have been explicitly provided access to the site and any users who have inherited access from a group.

sharepoint online powershell get all users in site collection

While my other post addresses How to Get All Users and Groups in SharePoint Online?, this post aims to get all users of the SharePoint 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

To get all users from the site collection using PowerShell, you will need to connect to your SharePoint Online site from PowerShell first. Once you are connected, you can run the Get-SPOUser cmdlet to return all users who have access to the site. Here is the SharePoint Online Management Shell way to get all users from all site collections in the tenant using the Get-SPOUser cmdlet:

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 

This script will gather all the users on the specified site and return them in an easy-to-read format.

#Config Variables
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
  
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive 
 
#Get All users of the site collection
Get-PnPUser 

This script gets you all users from the current site collection, regardless of whether they have 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 -Interactive 
$Web = Get-PnPWeb
 
#Get All users who have permission to the subsite 
Get-PnPUser -WithRightsAssigned -Web $Web 
Get All users from the site – Filter System users, Groups and get users with Email
$Users = Get-PnPUser | Where { $_.IsHiddenInUI -eq $false -and $_.PrincipalType -eq “User” -and (-not[string]::IsNullOrEmpty($_.Email))}

SharePoint Online: Export All Users in a Site Collection to CSV using PnP PowerShell

Let’s use the Get-PnPUser cmdlet to compile all users in the site collection and then export it to CSV for further analysis.

#Config Variables
$SiteURL = "https://crescent.sharepoint.com/"
$CSVFile = "C:\Temp\UserData.csv"
  
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-credential)  #-Interactive 
 
#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

This script connects to your SharePoint Online site collection with PowerShell, gets the user list from the site collection, and then exports the user list as CSV! And the script output:

sharepoint online powershell get all users in site collection

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

3 thoughts on “SharePoint Online: Get All Users in Site Collection using PowerShell

  • Get-PnPUser -WithRightsAssigned is timing out for me for several larger sites, is there any work around to using the Get-PnPUser?

    Reply
  • That is incorrect:
    SharePoint will return all users who have been explicitly provided access to the site and any users who have inherited access from a group.

    It will simply return ALL users existing in SP, nothing to do with site

    Reply
  • I am having same kind of issue and We are using the below scripts

    Connect-SPOService -Url https://tenant-admin.sharepoint.com

    $allsites = Get-SPOSite -Limit ALL

    $allsites| select Url,Title,LastContentModifiedDate,StorageUsageCurrent,Owner,StorageQuota,StorageQuotaWarningLevel,SharingCapability | Export-csv ‘.\SharePoint\AllSiteCollectionReport.csv’ -NoTypeInformation -Encoding UTF8

    #Grant SCA access to admin account
    $allsites | foreach{
    Set-SPOUser -Site $_.Url -LoginName $SVCDetails.Username -IsSiteCollectionAdmin $true
    }

    #Get All Users on all sites
    $AllUsers = @()
    $allsites | ForEach {
    try{
    $SiteUrl=$_.Url
    $siteUsers= Get-SPOUser –Site $SiteUrl -Limit ALL
    Sleep -Seconds 5
    Write-host “Site : “$SiteUrl “Total Number of users Found:”$siteUsers.Count
    ForEach($user in $siteUsers)
    {
    $user.Groups | foreach {
    $curGroup=$_
    $AllUsers += New-Object PSObject -Property @{
    ‘Site URL’ = $SiteUrl
    ‘Display Name’ = $user.DisplayName
    ‘Login Name’ = $user.LoginName
    ‘Group’ = $curGroup
    ‘User Type’ = $user.UserType
    ‘IsSiteAdmin’ =$user.IsSiteAdmin
    }
    }
    }
    }catch{
    Write-Host $_.Exception
    }
    $AllUsers | Export-Csv “.\SharePoint\AllSiteUsers.csv” -Encoding UTF8 -NoTypeInformation
    }

    Disconnect-SPOService

    }
    catch
    {
    Write-Host $_.Exception
    }

    Issue: We want to replacement the scripts with PNPOnline since our our account MFA enabled.

    Please help to get the replacement scripts for this

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *