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. $ConsoleWindow = $env:COMPUTERNAME
  15. $MyDateTimeFormat = "yyyyMMdd-HHmmss"
  16. $MyExecutionPath = (Get-Location).Path
  17. $MyScriptName = $MyInvocation.MyCommand.Name
  18. $MyLogFile = "$MyExecutionPath\_log\$MyScriptName-$ConsoleWindow-" + (Get-Date -Format $MyDateTimeFormat) + ".log"
  19. $MyOutputFile = "$MyExecutionPath\_output\$MyScriptName-$ConsoleWindow-" + (Get-Date -Format $MyDateTimeFormat) + ".csv"
  20. Set-Location $MyExecutionPath
  21. New-Item -ItemType Directory -Path "$MyExecutionPath\_log" -Force
  22. New-Item -ItemType Directory -Path "$MyExecutionPath\_output" -Force
  23. Start-Transcript -Path $MyLogFile -Append
  24. #Variables
  25. $UserDataCollection = @()
  26. }
  27. Process
  28. {
  29. Try
  30. {
  31. $AllWebs = Get-SPWebApplication $WebApplicationURL | Get-SPSite -Limit All | Get-SPWeb -Limit All | Select URL
  32. $itmCount = 0
  33. foreach($itm in $AllWebs)
  34. {
  35. $Web = Get-SPWeb $itm.URL
  36. if ($Web)
  37. {
  38. $itmCount ++
  39. Write-Progress -Id 1 -Activity 'Processing Webs' -Status "Current Web Count: $itmCount" -CurrentOperation $itm.URL -PercentComplete (($itmCount / $AllWebs.Count) * 100)
  40. Write-Host 'Processing site for users to export: ' $itm.URL
  41. $UsersColl = $Web.AllUsers
  42. $UserCount = 0
  43. Write-Host 'Processing ' $UsersColl.Count ' users on site ' $itm.URL
  44. foreach ($User in $UsersColl)
  45. {
  46. Write-Progress -Id 2 -ParentId 1 -Activity 'Processing Users' -Status "Current Count: $UserCount" -CurrentOperation $user.UserLogin.Tostring() -PercentComplete (($UserCount / $UsersColl.Count) * 100)
  47. if($User.IsDomainGroup -eq $false)
  48. {
  49. $UserData = New-Object PSObject
  50. $UserData | Add-Member -type NoteProperty -name "UserLogin" -value $user.UserLogin.ToString()
  51. $UserData | Add-Member -type NoteProperty -name "DisplayName" -value $user.displayName.ToString()
  52. $UserData | Add-Member -type NoteProperty -name "E-mailID" -value $user.Email.ToString()
  53. $UserDataCollection += $UserData
  54. $UserCount ++
  55. }
  56. }
  57. }
  58. }
  59. }
  60. Catch
  61. {
  62. Write-Error "Exception Type: $($_.Exception.GetType().FullName)"
  63. Write-Error "Exception Message: $($_.Exception.Message)"
  64. Write-Error "Exception Invocation: $($_.InvocationInfo)"
  65. }
  66. Finally
  67. {
  68. $UserDataCollection = $UserDataCollection | sort-object -Property {$_.UserLogin } -Unique
  69. $UserDataCollection | Export-Csv $MyOutputFile -NoTypeInformation
  70. Write-Output "Total Number of Unique Users found:"$UserDataCollection.Length
  71. Stop-Transcript
  72. }
  73. }
  74. }
  75. #Change the URL here to the WebApplication you wish to process
  76. 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! :)