Get all sharepoint users users in the farm with Powershell to a CSV file

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 is always appended to the file name so you don’t have to worry about wiping out previous results.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#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()
		}
 
	}
}

One thought on “Get all sharepoint users users in the farm with Powershell to a CSV file”

Comments are closed.