Get all Users in CSV File from SharePoint Farm using PowerShell

This PowerShell SharePoint tutorial explains, how to get all the SharePoint users in CSV file presented in farm using PowerShell in SharePoint 2013/2016.

This is a script that gets each SharePoint site on the farm, enumerates all the site collections and webs and dumps them to the screen as well as a CSV file.

The current date and time are always appended to the file name so you don’t have to worry about wiping out previous results.

Get all users from SharePoint Farm using PowerShell

Below is the PowerShell Script to Get All the Users from the SharePoint farm. The PowerShell script, we can debug or test using PowerShell ISE or using Visual Studio Code.

#getalluserseverywhere
Add-PSSnapin microsoft.sharepoint.powershell -ErrorAction SilentlyContinue
$timestamp = get-date -format "yyyyMMdd_hhmmtt"
$filenameStart = "AllFARMUsers"
$logfile = ("{0}{1}.csv" -f $filenamestart, $timestamp)
$header = "type,user,group,weburl,webname"
$header | out-file -FilePath $logfile
$iissitelist = get-spwebapplication
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 "domain\*")
#{
write-host " " $userw -foregroundcolor white
#$msg = ("{0},{1} user:{2}" -f $web.url,$web.name, $userw)
$msg = ("RootUser,{0},-,{1},{2}" -f $userw, $web.url,$web.name)
$msg | out-file -FilePath $logfile -append
# }
}
foreach ($group in $web.Groups)
{
Write-host " " $web.url $group.name: -foregroundcolor green
foreach ($user in $group.users)
{
# if ($user -like "Domain\*")
#{
Write-host " " $user -foregroundcolor white
#$msg = ("{0},{1},group:{2}, user:{3}" -f $web.url, $web.name, $group, $user)
$msg = ("GroupUser,{0},{1},{2},{3}" -f $user, $group, $web.url, $web.name)
$msg | out-file -FilePath $logfile -append
#}
}
}
$web.Dispose()
}
}
}

You may like following SharePoint PowerShell tutorials:

Here, we learned how to get all the SharePoint users in CSV file format from the SharePoint farm using PowerShell.

Donwload Hub site pdf

Download SharePoint Online Tutorial PDF FREE!

Get update on Webinars, video tutorials, training courses etc.

>