I'm looking to delete bulk (300+) Site groups (limited access users) that exist in a site. Powershell seems to be the fastest option, however I'm not great with it and I'm looking to have a script that doesnt delete all groups.

I want the Powershell script to delete all groups except for those which I specify... e.g. loop through all site groups and delete them all, except the groups I specify "My Group Name", "Restricted Readers", etc.

I found a sample script below where you have to specify which groups you want delete.. I want the opposite of this, where I can delete all groups except a set of groups with a certain name..

Example script

$spWeb = Get-SPWeb "https://mysite"
$spGroups = $spWeb.SiteGroups

$groups = ("Group 1", "Group 2","Group 3","Group 4","Group 5","Group 6")

ForEach($group in $groups) {


Edit: I want to delete the application of these groups to the site, e.g. what is achieved when you select the group and click "Remove User Permissions" in the UI


So you want to keep the groups and remove Role Assignments, right?

Try this one:

$web = Get-SPWeb https://server/sites/test
$keep = 'Group 1', 'Group 2'
$remove = $web.RoleAssignments | ? Member -NotIn $keep | % { $_.Member }
$remove | % { $web.RoleAssignments.Remove($_) }

Or a little longer version:

$web = Get-SPWeb https://server/sites/test
$keep = 'Group 1', 'Group 2'
$remove = $web.RoleAssignments | Where-Object { $_.Member -NotIn $keep } | ForEach-Object { $_.Member }
$remove | ForEach-Object { $web.RoleAssignments.Remove($_) }

For Office 365:

$siteUrl = 'https://test.sharepoint.com/sites/test'
$keep = 'Group 1', 'Group 2'
Connect-SPOService -Url 'https://test-admin.sharepoint.com' #tenant admin URL
$site = Get-SPOSite $siteUrl
Get-SPOSiteGroup -Site $site | ? LoginName -NotIn $keep |
  % { Set-SPOSiteGroup -Site $site -Identity $_.LoginName -PermissionLevelsToRemove $_.Roles }
  • Also wanted to ask.. the above script will work fine with SP Online? – Ushka Sep 9 '15 at 1:08
  • Also, is there a limit on the amount of groups that can have Role Assignments removed? (e.g. when I tried from UI it failed to remove role assignments for > 100 groups) – Ushka Sep 9 '15 at 1:59
  • I've added O365 version - it is quite different than on prem. I'm not aware of any limitations either in UI or scripts. – dstarkowski Sep 14 '15 at 11:44

You could try this:

$spWeb = Get-SPWeb "https://mysite"
$spGroups = $spWeb.SiteGroups

Write-Host "This site has" $spGroups.Count "groups"

$doNotDeleteGroupNames = ("Approvers", "Group 2","Group 3","Group 4","Group 5","Group 6")

$groups = $spGroups | ? {$_.Name -notin $doNotDeleteGroupNames}
Write-Host "Found" $groups.Count "groups which will be deleted:"

ForEach($group in $groups) {
   Write-Host "Deleting" $group.Name "..."

  • This will remove the groups. Expected result is the same as Remove User Permissions in UI, so only permissions should be removed. – dstarkowski Sep 8 '15 at 12:04
  • ok, then i did not understand him correctly, your answer seems correct then – Nils Sep 8 '15 at 12:06
  • Thanks Nils, I was seeking for exact this powershell script! – Fabian Oct 9 '15 at 11:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.