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.
In below few code Snippets we will see, How to get all users from 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()
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()
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.
- Using SharePoint Online Management Shell (SPO) PowerShell scripts
- with the CSOM scripts and
- 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.
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.
- Retrieve all Communication Sites using PnP PowerShell
- SharePoint Online: Delete Folders using PnP PowerShell
- How to Convert Site Page into News Post in SharePoint Online?
- Programmatically Upload a File in SharePoint Document Library.
- PowerShell to create Team Site in SharePoint Online
- Create a Communication Site using PowerShell
- Move SharePoint Online Files with PowerShell
- SharePoint Administrator Interview Questions
- MS-Flow Interview Questions and Answers
I hope you find this article interesting, If yes then please let me know by user comments below.
2 thoughts on “Retrieve all users from Site Collection using PowerShell”