Sunday, April 19, 2015

Web Application : Retrieve All Users Assigned to Groups in Site Collections (SP2007)

Issue:

You are requested to retrieve all users in all groups inside all site collections under a web application in SP2007. However SP2007 does not have Microsoft.SharePoint.Powershell assembly in it.

Solution:

Run this PowerShell script in Windows Shell

[System.Reflection.Assembly]::Load('Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c') | Out-Null

#Header
"SiteCollectionURL;GroupName;PermissionLevel;UserName;UserID"

#Specify the root site collection within the Web app
$Siteurl="http://contoso.com"; #change to your root site collection
$Rootweb=New-Object Microsoft.Sharepoint.Spsite($Siteurl);
$Webapp=$Rootweb.Webapplication;

#Loops through each site collection within the Web app
Foreach ($site in $Webapp.Sites)
{
    #Get all Site Collection Administrator
    foreach ($siteAdmin in $site.RootWeb.SiteAdministrators)
    {
        $site.URL+";Site Administrator;Full Control;"+$siteAdmin.Name+";"+$siteAdmin.Email
    }
    
    $groups = $site.RootWeb.SiteGroups
    foreach ($group in $groups) 
    {
        foreach ($user in $group.users) 
        {
            $site.URL+";"+$group.Name+";"+$group.Roles+";"+$user.Name+";"+$user.Email
        } 
    }
    
    $site.Dispose()
};

Usually I will just simply save the code as in PS1 file and use this command to save it into CSV file.

file.ps1 > result.csv

And then use Excel to massage the data accordingly. Have fun!

For SP2010 and SP2013 solution, click here.