Get All Users From Farm Level Into XML in SharePoint 2010

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.  
  9. #Header Added   
  10. $header = "All Farm Users"  
  11. $header | out-file -FilePath $logfile  
  12. $userDetailsArray = @()  
  13. $finaluserDetailsArray = @()  
  14. #Calling the list and Connecting the Farm   
  15. $iissitelist = get-spwebapplication(“Give your Farm level Site Url")   
  16. foreach($onesite in $iissitelist)  
  17. {  
  18.     foreach ($SiteCollection in $onesite.sites)  
  19.     {  
  20.         write-host $SiteCollection -foregroundcolor Blue   
  21.         foreach ($web in $SiteCollection.Allwebs)  
  22.         {   
  23.             write-host " " $web.url $web.name "users:" -foregroundcolor yellow  
  24.             # Write-host " " $web.users | select name   
  25.             foreach ($userw in $web.users)  
  26.             {  
  27.                 if ($userw -like "yourdomain\*")  
  28.                 {  
  29.                     write-host " " $userw -foregroundcolor white   
  30.                     $userwLogin = $userw.LoginName  
  31.                     $userDetailsArray =$userDetailsArray + $userwLogin   
  32.                 }  
  33.             }   
  34.             $web.Dispose()  
  35.         }  
  36.     }  
  37. }  
  38. #Tagging them to an array  
  39. $userArray = $userDetailsArray | select -unique   
  40. foreach ($value in $userArray) {  
  41.     #Result to xml File   
  42.     "$value" | out-file -FilePath $logfile -append   

  • You just need to change the Farm Level URL and no other change is required.
  • Now save this file as a .ps1 in Notepad selecting All Files.
  • Run it on the Windows PowerShell Modules.
  • You will see the users reflecting on the window from various site collections, sites and sub sites.
  • It will generate an .xml file to the same location with all Unique users.
  • Yes all Unique Users so you don't have double names coming up.
  • There you go, saving a lot of time and effort.

Keep learning!

Cheers.