Hello SharePointer,
Today, in this article we will see how to retrieve all users from site collection using PowerShell.

This tutorial will guide you to retrieve users from SharePoint on-premise as well as SharePoint Online. we also might need to download a list of users in CSV format.

For SharePoint On-Premise

In below few code Snippets we will see, How to get all users from SharePoint Site Collection.

PowerShell to get all users in SharePoint Site Collection

Below is the PowerShell that will display all users from SharePoint Site Collection on screen.

Add-PSSnapin microsoft.sharepoint.powershell -ErrorAction SilentlyContinue
$siteCollURL = "http://<your sitecollection URL>"
$site = new-object Microsoft.SharePoint.SPSite($siteCollURL)
$web = $site.openweb()
$siteUsers = $web.SiteUsers
Write-Host "Site Collection URL:" , $siteCollURL
foreach($user in $siteUsers)
{
write-host $user.LoginName
}
$web.Dispose()
$site.Dispose()

Download all users from SharePoint site collection using PowerShell

By using a below PowerShell Script, you can download all users from SharePoint site collection in CSV formatted file.

Add-PSSnapin microsoft.sharepoint.powershell -ErrorAction SilentlyContinue
$siteCollURL = "http://<your site collection URL>"
$site = new-object Microsoft.SharePoint.SPSite($siteCollURL)
$web = $site.openweb()
$siteUsers = $web.SiteUsers
Write-Host "Site Collection URL:" , $siteCollURL
$Output = foreach($user in $siteUsers)
{
New-Object -TypeName PSObject -Property @{
UserName = $user.LoginName
} | Select-Object UserName
}
$Output | Export-Csv E:\AllUser.csv -NoTypeInformation -Force
$web.Dispose()
$site.Dispose()

For SharePoint Online

In this section, we will see how to get all users from SharePoint Online site collection using PowerShell.

There are multiple methods or ways to retrieve all users from SharePoint Online site collection. These are described as below.

  1. Using SharePoint Online Management Shell (SPO) PowerShell scripts
  2. with the CSOM scripts and
  3. Using PnP PowerShell

In this tutorial, we are going to retrieve users from the SharePoint Online site using PnP PowerShell.

Below is the code to get all users using Pnp-PowerShell.

$SiteURL = "https://<your-tenant>.sharepoint.com/sites/HRD"
 
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -UseWebLogin

#Get All users of the site collection
Get-PnPUser

This will list all the users as below.

Retrieve all users from Site Collection using PowerShell

These are the ways to list all users from SharePoint Site Collection using PowerShell.

Also, you can find some More PowerShell script examples related to SharePoint.

I hope you find this article interesting, If yes then please let me know by user comments below.

One thought on “Retrieve all users from Site Collection using PowerShell”

Leave a Reply