create a list of groups and the users in each group

As we all know, digging through SharePoint groups looking to see what users are in each, is no fun.

To solve this, I created a new List, and added the groups I wanted to monitor.  Then, added a little PowerShell to update a column in the List with the members in each group.

Setup:

Create a new List.

New Columns:

SPgroup 

Type: Person or Group

Allow selection of: People or Group

Users

Type: Multiple lines of text

Specify the type of text to allow: 

Update Column:

Title

Require that this column contains information: No

The PowerShell script below does the following:

Get the SharePoint Site and List.

Loop through each item in the List.

Retrieve the value of the SPgroup field.

Truncate the Users field.

Loop through the users in the SPgroup.

Update the Users field with the values from SPgroup.


if(-not(Get-PSSnapin | where { $_.Name -eq "Microsoft.SharePoint.PowerShell"}))
{
      Add-PSSnapin Microsoft.SharePoint.PowerShell;
}

$sSite = Get-SPweb "http://reinmoto.com/sites/taco"
$sList=$sSite.Lists["SP Group Permissions"]
$i = 0

foreach($item in $sList.Items)
{
	$sFieldValue = $item["SPgroup"].ToString().Split('#')[1]
	$sGroup = $sSite.SiteGroups[$sFieldValue]

	If($sGroup.Users.Count -ge 0)
	{
		$item["Users"] = ""
		$item.Update()

		foreach($usr in $sGroup.Users)
			{
				if($i -eq 0)
				{
					$item["Users"] = $usr.DisplayName
				}
				else
				{
					$item["Users"] = $item["Users"] + ", " + $usr.DisplayName
				}
				$item.Update()
				$i=$i+1
			}
		$i=0
	}
}
$sSite.dispose()

Save the PowerShell script to your server.

Create a scheduled task to run the script hourly, daily, weekly, monthly…

Leave a Reply

Your email address will not be published. Required fields are marked *