Skip to content

Instantly share code, notes, and snippets.

@estruyf
Created May 2, 2014 12:30
Show Gist options
  • Save estruyf/b0ed8e4e26332490cb08 to your computer and use it in GitHub Desktop.
Save estruyf/b0ed8e4e26332490cb08 to your computer and use it in GitHub Desktop.
$site = Get-SPSite -Identity "http://YourSiteCollectionUrl"
# Retrieve used SharePoint groups
$groupsInUse = $site.AllWebs | % {$_.RoleAssignments | Select-Object Member}
$groupsInUse = $groupsInUse | % {$_.member.Tostring()}
$groupsInUse = $groupsInUse | Sort-Object | Get-Unique
$groupsInUse = $groupsInUse | ? {$_ -notmatch '\\' -and !$_.ToString().StartsWith('c:0(.s|') }
# Retrieve all SharePoint groups
$allGroups = $site.RootWeb.SiteGroups | Select-Object name
$allGroups = $allGroups |% {$_.name.Tostring()}
$allGroups = $allGroups |Sort-Object|Get-Unique
# Compare groups
$groupsNotInUse = Compare-Object $allGroups $groupsInUse
$groupsNotInUse = ($groupsNotInUse|Select-Object InputObject)
$unusedGroups = $groupsNotInUse | % {$_.InputObject.Tostring()}
# Print unused groups
Write-host $unusedGroups | Write-Host $_
# Remove unused groups
$allGroups = $site.RootWeb.SiteGroups
$unusedGroups | % {$allGroups.Remove($_)}
$site.Dispose()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment