Background:
I wanted to see all users who have access to the SharePoint site and belongs to which SharePoint group.
Solution:
Below SharePoint powershell script helps me to get list of users group wise-
$site = Get-SPSite <Provide Site Collection URL here>
$web = $site.OpenWeb()
$groups = $web.sitegroups
Â
foreach
($grp
in
$groups) {
   Â
"Group: "
+ $grp.name;
   Â
$groupName = $grp.name
   Â
write-host
"Group: "
$groupName  -foregroundcolor green
   Â
foreach
($user
in
$grp.users) {
           Â
"User: "
+ $user.name
           Â
write-host
"User "
$user.UserLogin  -foregroundcolor red
   Â
}
}
Usage:
- Navigate to Script location and type below command-
GetUsers.ps1
This will show output on screen.
- Navigate to Script location and type below command-
GetUsers.ps1 | Out-File C:Output.csv
This will generate output as csv file. “C:Output.csv” is the file name and location where generated csv file is saved. Please change as per your requirement.
Categories: SharePoint 2019