Get All Users In A SharePoint 2010/13/16 Farm Using PowerShell Script

Welcome to an article on how to get all users in a SharePoint 2010/13/16 farm using PowerShell Script. This script is going to run and loop in all the web applications and site collections within them and get individual and group users.

This script will help save us developers a lot of time in getting all the users from an individual or group. So, here is the script.

  • Copy the code below to a .ps1 file.
  • Run the .ps1 file on the SharePoint PowerShell modules.
  • You don't need to do any update on the script.

Script

  1. #getalluserinthefarm  
  2. Add-PSSnapin microsoft.sharepoint.powershell -ErrorAction SilentlyContinue  
  3.    
  4. $Currentime = get-date -format "yyyyMMdd_hhmmtt"  
  5. $filename = "FarmUsers"  
  6. $datafile = ("{0}{1}.csv" -f $filename, $Currentime)  
  7.    
  8. $headerfile = "type,user,group,weburl,webtitle"  
  9. $headerfile | out-file -FilePath $datafile  
  10.    
  11. $iissitedata = get-spwebapplication   
  12. foreach($farmsite in $iissitedata)  
  13. {  
  14.    
  15.     foreach ($SiteCollection in $farmsite.sites)  
  16.     {  
  17.         write-host $SiteCollection -foregroundcolor Blue      
  18.         foreach ($web in $SiteCollection.Allwebs)  
  19.         {   
  20.              write-host "    " $web.url $web.name "users:" -foregroundcolor yellow  
  21.              foreach ($usersite in $web.users)  
  22.              {        
  23.                     write-host "        " $usersite -foregroundcolor white  
  24.                     $data = ("RootUser,{0},-,{1},{2}" -f $usersite, $web.url,$web.name)   
  25.                     $data | out-file -FilePath $datafile  -append  
  26.                 }  
  27.     
  28.              foreach ($group in $web.Groups)  
  29.             {  
  30.                  Write-host "        " $web.url $group.name: -foregroundcolor green  
  31.                  foreach ($user in $group.users)  
  32.                  {   
  33.                           Write-host "            " $user -foregroundcolor white  
  34.                           $data = ("GroupUser,{0},{1},{2},{3}" -f $user, $group, $web.url, $web.name)  
  35.                           $data | out-file -FilePath $datafile  -append  
  36.                  }  
  37.             }     
  38.             $web.Dispose()  
  39.         }  
  40.    
  41.     }  
  42. }  

Once you initiate the script, it will run through all site collections in all the webs and populate the date and save it in on the same location you are running it from, in a .csv or .txt format.

Just run this script and you will get all the users on the farm.

Keep reading & keep learning!