Export Unique Users from a SharePoint Web Application

Introduction

Here, I have written a Sharepoint PowerShell Script for exporting unique Sharepoint Users from an SP web application. This will export unique users. I have written the output as a CSV file in the _output folder. If any error occurs, it writes logs in _log folder
 
Script: 
  1. <#  
  2.     Create a directory called 'Get-ALLSPUsers' and execute this script from that directory.  
  3. #>  
  4. function Get-AllSPUsers{  
  5.     [CmdletBinding()]  
  6.     Param  
  7.     (  
  8.         [string]$WebApplicationURL  
  9.     )  
  10.     Begin  
  11.     {  
  12.         Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue  
  13.         [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")  
  14.   
  15.         $ConsoleWindow = $env:COMPUTERNAME  
  16.         $MyDateTimeFormat = "yyyyMMdd-HHmmss"  
  17.         $MyExecutionPath = (Get-Location).Path  
  18.         $MyScriptName = $MyInvocation.MyCommand.Name  
  19.         $MyLogFile = "$MyExecutionPath\_log\$MyScriptName-$ConsoleWindow-" + (Get-Date -Format $MyDateTimeFormat) + ".log"  
  20.         $MyOutputFile = "$MyExecutionPath\_output\$MyScriptName-$ConsoleWindow-" + (Get-Date -Format $MyDateTimeFormat) + ".csv"  
  21.         Set-Location $MyExecutionPath  
  22.         New-Item -ItemType Directory -Path "$MyExecutionPath\_log" -Force  
  23.         New-Item -ItemType Directory -Path "$MyExecutionPath\_output" -Force          
  24.         Start-Transcript -Path $MyLogFile -Append   
  25.         #Variables  
  26.         $UserDataCollection = @()   
  27.     }  
  28.     Process  
  29.     {  
  30.         Try  
  31.         {  
  32.             $AllWebs = Get-SPWebApplication $WebApplicationURL | Get-SPSite -Limit All | Get-SPWeb -Limit All | Select URL  
  33.             $itmCount = 0  
  34.   
  35.             foreach($itm in $AllWebs)  
  36.             {  
  37.                 $Web = Get-SPWeb $itm.URL  
  38.                 if ($Web)  
  39.                 {  
  40.                     $itmCount ++   
  41.                     Write-Progress -Id 1 -Activity 'Processing Webs' -Status "Current Web Count: $itmCount" -CurrentOperation $itm.URL -PercentComplete (($itmCount / $AllWebs.Count) * 100)     
  42.                     Write-Host 'Processing site for users to export: ' $itm.URL  
  43.                     $UsersColl = $Web.AllUsers  
  44.                     $UserCount = 0  
  45.                     Write-Host 'Processing ' $UsersColl.Count ' users on site ' $itm.URL  
  46.   
  47.                     foreach ($User in $UsersColl)  
  48.                     {  
  49.                           
  50.                         Write-Progress -Id 2 -ParentId 1 -Activity 'Processing Users' -Status "Current Count: $UserCount" -CurrentOperation $user.UserLogin.Tostring() -PercentComplete (($UserCount / $UsersColl.Count) * 100)  
  51.   
  52.                         if($User.IsDomainGroup -eq $false)   
  53.                         {  
  54.   
  55.                             $UserData = New-Object PSObject  
  56.                             $UserData | Add-Member -type NoteProperty -name "UserLogin" -value $user.UserLogin.ToString()  
  57.                             $UserData | Add-Member -type NoteProperty -name "DisplayName" -value $user.displayName.ToString()  
  58.                             $UserData | Add-Member -type NoteProperty -name "E-mailID" -value $user.Email.ToString()  
  59.                             $UserDataCollection += $UserData  
  60.                             $UserCount ++  
  61.                         }  
  62.                     }  
  63.                 }  
  64.             }  
  65.         }  
  66.         Catch  
  67.         {  
  68.             Write-Error "Exception Type: $($_.Exception.GetType().FullName)"  
  69.             Write-Error "Exception Message: $($_.Exception.Message)"  
  70.             Write-Error "Exception Invocation: $($_.InvocationInfo)"  
  71.         }  
  72.         Finally  
  73.         {  
  74.             $UserDataCollection = $UserDataCollection | sort-object -Property {$_.UserLogin } -Unique  
  75.             $UserDataCollection | Export-Csv $MyOutputFile -NoTypeInformation  
  76.             Write-Output "Total Number of Unique Users found:"$UserDataCollection.Length  
  77.             Stop-Transcript  
  78.         }  
  79.     }  
  80. }  
  81.  
  82. #Change the URL here to the WebApplication you wish to process  
  83. Get-AllSPUsers -WebApplicationURL "Your WebApplication Url" -Verbose  
 Save this file as a .ps1 extension and run it in your Sharepoint management shell.
 
 Hope this helps someone! :)