How to get all users in a group in SharePoint (PowerShell)

Introduction

This Script shows how to get all users in a group in SharePoint.

Scenarios

Sometime users want to know how many users in a specified group. This script is to achieve that goal.

Script

You can use this script in the following way:

Method:

  1. Download the script and open the script file with Notepad or any other script editors.
  2. Scroll down to the end of the script file, and then add the example command which you want to run.
  3. Save the file then run the script in PowerShell.

Examples

Example 1: Get all users in a site.

Command: C:\Script\groupmember.ps1 -SiteUrl "http://win-antqs3uheke/sites/site1
Screenshot:

Example 2: Get all users in specified group.

Command: C:\Script\groupmember.ps1 -SiteUrl "http://win-antqs3uheke/sites/site1 -GroupName "site1 Members"
Screenshot:

Here are some code snippets for your references.

PowerShell
Edit|Remove
#Define an empty array
$result = @()
#Get the specified site
$site = Get-SPSite -Identity $SiteUrl
#Open the site
$web=$site.OpenWeb()
#Check if the groupname is empty
If($Groupname)
{
    $Groups=$web.Groups["$Groupname"]
}
Else
{
    $Groups=$web.Groups
}

Prerequisite

Windows Server 2008 or higher versionMicrosoft All-In-One Script Framework is an automation script sample library for IT Professionals. The key value that All-In-One Script Framework is trying to deliver is Scenario-Focused Script Samples driven by IT Pros' real-world pains and needs. The team is monitoring all TechNet forums, IT Pros' support calls to Microsoft, and script requests submitted to TechNet Script Repository. We collect frequently asked IT scenarios, and create script samples to automate the tasks and save some time for IT Pros. The team of All-In-One Script Framework sincerely hope that these customer-driven automation script samples can help our IT community in this script-centric move.