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.

Script

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

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!