In this SharePoint PowerShell example we will discuss how to get all users from SharePoint site collection using PowerShell. We will also download all users in CSV format from site collection using PowerShell script.
Also, we will check a user is an admin or not in the PowerShell script in SharePoint 2016/2013. SharePoint PowerShell get all users in site collection.
You can write, run, and debug PowerShell scripts using Windows PowerShell ISE or Visual Studio code.
SharePoint Online tutorial contents:
The below PowerShell script will explain, how to retrieve all users from SharePoint site collections using PowerShell and also it will display whether the particular user is an admin or a normal site user.
Add-PSSnapin microsoft.sharepoint.powershell -ErrorAction SilentlyContinue
$siteCollURL = "http://mypc/sites/MySP2016SiteCollection"
$site = new-object Microsoft.SharePoint.SPSite($siteCollURL)
$web = $site.openweb()
$siteUsers = $web.SiteUsers
Write-Host "Site Collection URL:" , $siteCollURL
foreach($user in $siteUsers)
{
if($user.IsSiteAdmin -eq $true)
{
Write-Host "User Name: ", $user.LoginName , "Role: Admin"
}
else
{
Write-Host "User Name: ", $user.LoginName, "Role: User"
}
}
$web.Dispose()
$site.Dispose()
You can see the output after running the script.
Below is another PowerShell script that will display all users from SharePoint 2016 site collection without role.
Add-PSSnapin microsoft.sharepoint.powershell -ErrorAction SilentlyContinue
$siteCollURL = "http://mypc/sites/MySP2016SiteCollection"
$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 the below PowerShell script, you will be able to download all users (CSV format) from SharePoint 2016/2013 site collection using PowerShell script.
Add-PSSnapin microsoft.sharepoint.powershell -ErrorAction SilentlyContinue
$siteCollURL = "http://mypc/sites/MySP2016SiteCollection"
$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()
Also, you can read some more PowerShell SharePoint examples below:
- PnP PowerShell Commands for SharePoint Online List
- PnP PowerShell commands for SharePoint Online Site
- Get SharePoint document library size using PowerShell
- SharePoint Online Automation: Upload Files Remotely to SharePoint Online Document Library using PowerShell
- What is PowerShell variable
- SharePoint 2013 backup and restore using PowerShell
- Hide Document Library or Lists using PowerShell in SharePoint 2016/2013
- SharePoint Online storage limits
- How to send email using PowerShell in Office 365
- PowerShell find files modified in last N days
- Insert/Add item to SharePoint Online list using PowerShell
- Delete list items created before N days using PowerShell in SharePoint Online
Conclusion
I hope this article will be helpful to get all users in a site collection in SharePoint using PowerShell. We have seen here sharepoint PowerShell get all users in a site collection and how to download all users in CSV format from site collection using PowerShell in SharePoint 2016/2013.
I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site EnjoySharePoint.com