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:
- <#
- Create a directory called 'Get-ALLSPUsers' and execute this script from that directory.
- #>
- function Get-AllSPUsers{
- [CmdletBinding()]
- Param
- (
- [string]$WebApplicationURL
- )
- Begin
- {
- Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
- [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
-
- $ConsoleWindow = $env:COMPUTERNAME
- $MyDateTimeFormat = "yyyyMMdd-HHmmss"
- $MyExecutionPath = (Get-Location).Path
- $MyScriptName = $MyInvocation.MyCommand.Name
- $MyLogFile = "$MyExecutionPath\_log\$MyScriptName-$ConsoleWindow-" + (Get-Date -Format $MyDateTimeFormat) + ".log"
- $MyOutputFile = "$MyExecutionPath\_output\$MyScriptName-$ConsoleWindow-" + (Get-Date -Format $MyDateTimeFormat) + ".csv"
- Set-Location $MyExecutionPath
- New-Item -ItemType Directory -Path "$MyExecutionPath\_log" -Force
- New-Item -ItemType Directory -Path "$MyExecutionPath\_output" -Force
- Start-Transcript -Path $MyLogFile -Append
- #Variables
- $UserDataCollection = @()
- }
- Process
- {
- Try
- {
- $AllWebs = Get-SPWebApplication $WebApplicationURL | Get-SPSite -Limit All | Get-SPWeb -Limit All | Select URL
- $itmCount = 0
-
- foreach($itm in $AllWebs)
- {
- $Web = Get-SPWeb $itm.URL
- if ($Web)
- {
- $itmCount ++
- Write-Progress -Id 1 -Activity 'Processing Webs' -Status "Current Web Count: $itmCount" -CurrentOperation $itm.URL -PercentComplete (($itmCount / $AllWebs.Count) * 100)
- Write-Host 'Processing site for users to export: ' $itm.URL
- $UsersColl = $Web.AllUsers
- $UserCount = 0
- Write-Host 'Processing ' $UsersColl.Count ' users on site ' $itm.URL
-
- foreach ($User in $UsersColl)
- {
-
- Write-Progress -Id 2 -ParentId 1 -Activity 'Processing Users' -Status "Current Count: $UserCount" -CurrentOperation $user.UserLogin.Tostring() -PercentComplete (($UserCount / $UsersColl.Count) * 100)
-
- if($User.IsDomainGroup -eq $false)
- {
-
- $UserData = New-Object PSObject
- $UserData | Add-Member -type NoteProperty -name "UserLogin" -value $user.UserLogin.ToString()
- $UserData | Add-Member -type NoteProperty -name "DisplayName" -value $user.displayName.ToString()
- $UserData | Add-Member -type NoteProperty -name "E-mailID" -value $user.Email.ToString()
- $UserDataCollection += $UserData
- $UserCount ++
- }
- }
- }
- }
- }
- Catch
- {
- Write-Error "Exception Type: $($_.Exception.GetType().FullName)"
- Write-Error "Exception Message: $($_.Exception.Message)"
- Write-Error "Exception Invocation: $($_.InvocationInfo)"
- }
- Finally
- {
- $UserDataCollection = $UserDataCollection | sort-object -Property {$_.UserLogin } -Unique
- $UserDataCollection | Export-Csv $MyOutputFile -NoTypeInformation
- Write-Output "Total Number of Unique Users found:"$UserDataCollection.Length
- Stop-Transcript
- }
- }
- }
-
- #Change the URL here to the WebApplication you wish to process
- 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! :)