Welcome to an article on how to get all users in a SharePoint 2010/13/16 farm using PowerShell Script. This script is going to run and loop in all the web applications and site collections within them and get individual and group users.
This script will help save us developers a lot of time in getting all the users from an individual or group. So, here is the script.
- Copy the code below to a .ps1 file.
- Run the .ps1 file on the SharePoint PowerShell modules.
- You don't need to do any update on the script.
Script
- #getalluserinthefarm
- Add-PSSnapin microsoft.sharepoint.powershell -ErrorAction SilentlyContinue
-
- $Currentime = get-date -format "yyyyMMdd_hhmmtt"
- $filename = "FarmUsers"
- $datafile = ("{0}{1}.csv" -f $filename, $Currentime)
-
- $headerfile = "type,user,group,weburl,webtitle"
- $headerfile | out-file -FilePath $datafile
-
- $iissitedata = get-spwebapplication
- foreach($farmsite in $iissitedata)
- {
-
- foreach ($SiteCollection in $farmsite.sites)
- {
- write-host $SiteCollection -foregroundcolor Blue
- foreach ($web in $SiteCollection.Allwebs)
- {
- write-host " " $web.url $web.name "users:" -foregroundcolor yellow
- foreach ($usersite in $web.users)
- {
- write-host " " $usersite -foregroundcolor white
- $data = ("RootUser,{0},-,{1},{2}" -f $usersite, $web.url,$web.name)
- $data | out-file -FilePath $datafile -append
- }
-
- foreach ($group in $web.Groups)
- {
- Write-host " " $web.url $group.name: -foregroundcolor green
- foreach ($user in $group.users)
- {
- Write-host " " $user -foregroundcolor white
- $data = ("GroupUser,{0},{1},{2},{3}" -f $user, $group, $web.url, $web.name)
- $data | out-file -FilePath $datafile -append
- }
- }
- $web.Dispose()
- }
-
- }
- }
Once you initiate the script, it will run through all site collections in all the webs and populate the date and save it in on the same location you are running it from, in a .csv or .txt format.
Just run this script and you will get all the users on the farm.
Keep reading & keep learning!