How to remove SharePoint User Groups with PowerShell
For deleting SharePoint groups you can use SharePoint Designer. For this you should open site in SPDesigner, in left column goto "Site Groups", select groups for deleting and press Delete button on keyboard or in ribbon. But this process doesn't always work. And when I wanted to remove user groups SPDesigner hanged and reported error of deleting.
So I decided to use PowerShell for this. It's rather short but very heplful. Add to my instrumentary.
I selected user group names and added them to array $groups in cycle remove groups.
$spWeb = Get-SPWeb "https://spdev" $spGroups = $spWeb.SiteGroups $groups = ("Group 1", "Group 2","Group 3","Group 4","Group 5","Group 6") ForEach($group in $groups) { $spGroups.Remove($group) } $spWeb.Dispose()
Thanks for the script. There are some more possible ways to Delete Users from SharePoint Site Collection:
1. You can delete users using SharePoint Web Interface
2. Delete users from SharePoint site using PowerShell (Bulk Delete also possible)
3. Remove users from SharePoint programmatically using C#
Found these methods at SharePointDiary.com: Delete Users from SharePoint Site Collection
Comment by SPAdam — July 31, 2013 @ 8:55 am
Of course they are visible until you update Designer info (press F5) or close SPDesigner and open it again
Comment by admin — September 21, 2013 @ 11:07 am
Have you verified gorups in SharePoint designer after deleting groups using Powershell script?
Groups will be visible if you use powershell scripts to delete groups.
Comment by swamy — September 18, 2013 @ 3:27 am
I am getting an error using the code above:
An error occurred while enumerating through a collection: Collection was modifi
ed; enumeration operation may not execute..
The 1st group gets deleted but the loop stops after that error has been thrown.
Help please!
Thanks!
Comment by radc — October 21, 2013 @ 8:52 am
It’s a raw script – here’s no exceptions. I think you have the error because you don’t have a group with a name you want to delete and it’s a reason of error message. The second reason – try to set for variable $spWeb the site where the group you want to delete has permissions.
For example, $spWeb = “http://spdev/groupsite/
Comment by admin — October 22, 2013 @ 9:39 am
You need to “Check” the group exists first.
The problem is that you have an “array” of groups/groupnames, and once you delete the 1st group, that array has changed and is no longer valid.
Do something like this:
if($Web.SiteGroups[$GroupName] -ne $null)
{
$web.SiteGroups.Remove($GroupName)
$web.Update()
Write-Host “Group Deleted!”
}
else
{
Write-Host “Group doesn’t Exists!”
}
Comment by Josh — June 23, 2014 @ 7:18 pm