Powershell script to get all the MOSS groups a user belongs to for a web application

$path = “C:\Admin\”
$sites = get-spsite -webapplication “https://abc.com/”
$skipSites = “XX1″,”YY2”
$flag=1
foreach ($site in $sites) {
if($skipSites -notcontains $site.RootWeb.Title) {
[array]$users = $null
$name = $site.RootWeb.Title
$UserWebAccess = “C:\Admin\UserWebAccessList.csv”
$UserGroups = “C:\Admin\UserGroupsList.csv”
write-host -foregroundcolor green “Working on Site Collection: “$site.RootWeb.Title “…”
foreach ($web in $site.AllWebs) {
write-host -foregroundcolor yellow “–Working on web:” $web.Title “…”
foreach ($group in $web.groups){
write-host -foregroundcolor white “—-Collecting users from group:”$group.Name “…”
foreach($user in $group.users)
{

if ($user.Loginname.StartsWith(“i:0#.w|admin\xxxxxxxx”))
{
$users = new-object psobject
$users | add-member noteproperty -name “User” -value $user
$users | add-member noteproperty -name “Display Name” -value $user.DisplayName
$users | add-member noteproperty -name “Groups” -value $group.name
$users | add-member noteproperty -name “SiteName” -value $site.RootWeb.Title
$users | Add-Member NoteProperty -name “WebName” -value $web.Title
$users | add-member noteproperty -name “WebURL” -value $web.Url
$combinedusers += $users
if ($flag -eq 1)
{
$combinedgroups += $group.name
$combinedgroups += “, ”
$flag = 0
}
if (-not $combinedgroups.ToLower().Contains($group.name.ToLower()))
{
$combinedgroups += $group.name
$combinedgroups += “, ”
}
}
}
}
$web.Dispose()
}
$combinedusers | Out-File $UserWebAccess
$combinedusers = $null
$combinedgroups | Out-File $UserGroups
$combinedgroups = $null

}
}

Thanks to:

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s