Background:

Need to see all users who have access to the SharePoint site and that belongs to a SharePoint group.

Solution:

Below SharePoint PowerShell script helps to get a list of users by group:


$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 the screen.

  • Navigate to script location and type below command:

GetUsers.ps1 | Out-File C:\Output.csv

This will generate output as a csv file. "C:\Output.csv" is the file name and location is where generated csv file is saved. Please change as per your requirement.
 
The script can also be downloaded from TechNet gallery here.

Hope it helps someone.