Introduction

I needed to get all unique users from the Farm Level using PowerShell for my requirement. I thought it might be available in the web since it might be used many times by other developers but seriously I didn't get any proper one so I wrote it myself and thought of sharing it with you all.

So here is one more new article to get all the users in a Farm Level from all site collections, groups and whereever they are hidden in a Farm.

You can get the output into a text file, Excel sheet or XML files. You just need to change the extension. Here I will be moving them to XML.

So here is the script, kindly have a look at the commented lines for descriptions.

Script

  1. #get all users from Farm level
  2. #Snapin
  3. Add-PSSnapin microsoft.sharepoint.powershell -ErrorAction SilentlyContinue
  4. #File Output Name Generator( This will give a name to the xml with name as ALLFarm Users and Todays’s Current Date & Time)
  5. $timestamp = get-date -format "yyyyMMdd_hhmmtt"
  6. $filenameStart = "AllFARMUsers"
  7. $logfile = ("{0}{1}.xml" -f $filenamestart, $timestamp)
  8. #Header Added
  9. $header = "All Farm Users"
  10. $header | out-file -FilePath $logfile
  11. $userDetailsArray = @()
  12. $finaluserDetailsArray = @()
  13. #Calling the list and Connecting the Farm
  14. $iissitelist = get-spwebapplication(“Give your Farm level Site Url")
  15. foreach($onesite in $iissitelist)
  16. {
  17. foreach ($SiteCollection in $onesite.sites)
  18. {
  19. write-host $SiteCollection -foregroundcolor Blue
  20. foreach ($web in $SiteCollection.Allwebs)
  21. {
  22. write-host " " $web.url $web.name "users:" -foregroundcolor yellow
  23. # Write-host " " $web.users | select name
  24. foreach ($userw in $web.users)
  25. {
  26. if ($userw -like "yourdomain\*")
  27. {
  28. write-host " " $userw -foregroundcolor white
  29. $userwLogin = $userw.LoginName
  30. $userDetailsArray =$userDetailsArray + $userwLogin
  31. }
  32. }
  33. $web.Dispose()
  34. }
  35. }
  36. }
  37. #Tagging them to an array
  38. $userArray = $userDetailsArray | select -unique
  39. foreach ($value in $userArray) {
  40. #Result to xml File
  41. "$value" | out-file -FilePath $logfile -append
  42. }

Keep learning!

Cheers.