Get All Users of SharePoint Farm-Web Application-Site Collection-Site using PowerShell

Requirement: Get all users of SharePoint Farm.

sharepoint powershell get all users in farm

SharePoint PowerShell to Get All Users in Farm

Do you want to get a list of all users in your SharePoint farm? PowerShell can help you with this! This blog post will show you how to use PowerShell to quickly find all users in your SharePoint farm.

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Output Report File
$currentLocation = (Get-Location).Path
$outputReport = $currentLocation + "\" + "SharePointUsers.csv" 
#Write CSV File Header

#Array to hold user data
$UserDataCollection = @() 

#Get All Web Applications and iterate through
$WebAppsColl = Get-SPWebApplication 
#To Get all Users from specific web application, Use: $WeAppsColl = Get-SPWebApplication "web-app-url"
#and remove line #12
 
foreach($WebApp in $WebAppsColl)
{
    Write-host "Scanning Web Application:"$WebApp.Name
    #Get All site collections and iterate through
    $SitesColl = $WebApp.Sites
    #To Get all Users from site collection, Use: $SitesColl = Get-SPSite "site-collection-url"
    #and remove lines between #11 to #20 and Line #55 "}"
    #get all users from site collection PowerShell
    foreach ($Site in $SitesColl) 
    {
        Write-host "Scanning Site Collection:"$Site.URL
        #Get All Webs and iterate through
        $WebsColl = $Site.AllWebs
        #To Get all Users from aq site, Use: $WebsColl = Get-SPWeb "web-url"
         #and remove lines between #11 to #28 and Lines #53, #54, #55 "}"

            foreach ($web in $WebsColl) 
            {
                Write-host "Scanning Web:"$Web.URL
                #Get All Users of the Web
                $UsersColl = $web.AllUsers  #get all users programmatically 
                    #list all users 
                    foreach ($user in $UsersColl) 
                    {
                           if($User.IsDomainGroup -eq $false) 
                            {
                                $UserData = New-Object PSObject
              
                                $UserData | Add-Member -type NoteProperty -name "UserLogin" -value $user.UserLogin.ToString()
                                $UserData | Add-Member -type NoteProperty -name "DisplayName" -value $user.displayName.ToString()
                                $UserData | Add-Member -type NoteProperty -name "E-mailID" -value $user.Email.ToString()

                                $UserDataCollection += $UserData
                            }
                    }
            $Web.dispose()
            }
         $site.dispose()
        }
    }    
#Remove duplicates
$UserDataCollection = $UserDataCollection | sort-object -Property  {$_.UserLogin } -Unique 

#Remove duplicates and export all users to excel
$UserDataCollection | Export-Csv -LiteralPath $OutputReport -NoTypeInformation
Write-host "Total Number of Unique Users found:"$UserDataCollection.Length

This script can be used to get all users in site collection and export all users to excel.

Get All Unique Users in SharePoint Farm using PowerShell:

One liner PowerShell script to get unique users of the SharePoint farm:

Get-SPSite -Limit All | Select -ExpandProperty AllWebs | select -ExpandProperty AllUsers | ? {$_.IsDomainGroup -ne $true} |  select -Unique LoginName

Salaudeen Rajack

Salaudeen Rajack is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

7 thoughts on “Get All Users of SharePoint Farm-Web Application-Site Collection-Site using PowerShell

  • Saludeen —

    This is the closest I’ve come to what I’m looking for. What I need is to get all users of all subsites of all webs of all site collections of a web app in one CSV. I know you’ve got comments in the script to do this one web at a time, but I’m dealing with two huge webapps, one of which has one site collection with 2400 webs and over 7000 subsites. How can I modify this script to do this?

    Reply
  • Can you please assist to add SITEURL column to CSV in the above script

    Reply
  • I am running the below snippet, it errors as Exception has been thrown by the target of invocation:
    Kindly assist:

    Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
    $currentLocation = (Get-Location).Path
    $outputReport = $currentLocation + “” + “LngUniqueUsers.csv”

    $UserDataCollection = @()

    Get-SPSite -Limit All | Select -ExpandProperty AllWebs | select -ExpandProperty AllUsers | ? {$_.IsDomainGroup -ne $true} |  select -Unique LoginName

    $UserDataCollection | Export-Csv -LiteralPath $OutputReport -NoTypeInformation

    Reply
    • Which means: you don’t have enough permissions on the Site collections! Grant “Full Control” at web application level to your account and try rerun.

      Reply
  • hi, to “Get All Unique Users in SharePoint Farm using PowerShell:” do we just need to run this single line of script or amend it into the longer snippet above?

    Reply
  • Thank you, this was very helpful!

    Reply

Leave a Reply