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
- #get all users from Farm level
- #Snapin
- Add-PSSnapin microsoft.sharepoint.powershell -ErrorAction SilentlyContinue
- #File Output Name Generator( This will give a name to the xml with name as ALLFarm Users and Todays’s Current Date & Time)
- $timestamp = get-date -format "yyyyMMdd_hhmmtt"
- $filenameStart = "AllFARMUsers"
- $logfile = ("{0}{1}.xml" -f $filenamestart, $timestamp)
-
- #Header Added
- $header = "All Farm Users"
- $header | out-file -FilePath $logfile
- $userDetailsArray = @()
- $finaluserDetailsArray = @()
- #Calling the list and Connecting the Farm
- $iissitelist = get-spwebapplication(“Give your Farm level Site Url")
- foreach($onesite in $iissitelist)
- {
- foreach ($SiteCollection in $onesite.sites)
- {
- write-host $SiteCollection -foregroundcolor Blue
- foreach ($web in $SiteCollection.Allwebs)
- {
- write-host " " $web.url $web.name "users:" -foregroundcolor yellow
- # Write-host " " $web.users | select name
- foreach ($userw in $web.users)
- {
- if ($userw -like "yourdomain\*")
- {
- write-host " " $userw -foregroundcolor white
- $userwLogin = $userw.LoginName
- $userDetailsArray =$userDetailsArray + $userwLogin
- }
- }
- $web.Dispose()
- }
- }
- }
- #Tagging them to an array
- $userArray = $userDetailsArray | select -unique
- foreach ($value in $userArray) {
- #Result to xml File
- "$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.