PowerShell script to remove all users from a SharePoint Group

PowerShell script to remove all users from a SharePoint Group

  • Comments 3

Moved from http://blogs.msdn.com/vijgang 

There have been many instances where we find users have added too many individual user accounts to a specific SharePoint group. This causes many issues like search failing, performance issues, etc.

################################################################################################################

[System.Reflection.Assembly]::Load("Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c")
[System.Reflection.Assembly]::Load("Microsoft.SharePoint.Portal, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c")
[System.Reflection.Assembly]::Load("Microsoft.SharePoint.Publishing, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c")
[System.Reflection.Assembly]::Load("System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")

###########################
# "Enter the site URL here"
$SITEURL = "http://serverurl"

# "Name of Site group from which users have to be removed"
$SITEGROUP = "Site_Contributors"

###########################

$site = new-object Microsoft.SharePoint.SPSite ( $SITEURL )
$web = $site.OpenWeb()
"Web is : " + $web.Title

$oSiteGroup = $web.SiteGroups[$SITEGROUP];

"Site Group is :" + $oSiteGroup.Name
$oUsers = $oSiteGroup.Users

foreach ($oUser in $oUsers)
{
    "Removing user : " + $oUser.Name
    $oSiteGroup.RemoveUser($oUser)
}

################################################################################################################

Disclaimer: The above code is provided "As Is". This is just a sample code and is not production ready.

Leave a Comment
  • Please add 3 and 4 and type the answer here:
  • Post
  • Thanks! This worked like a charm.

  • 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#

    Find these methods at SharePointDiary.com:  <a href="www.sharepointdiary.com/.../delete-users-from-site-collection-using-powershell.html Users from SharePoint Site Collection </a>

  • Thank you very much, it worked perfectly.  I added few edits to perform the operation on multiple groups at once, by storing the groups names in array. and after removing all users you can add the desired users again at the end of the loop. I hope it might be helpful to somebody :)

    # "Enter the site URL here"

    $SITEURL = "http://YourSite"

    # "Name of Site group from which users have to be removed"

    $SiteGroups=@("Group1","Group2","Group3")

    for($i=0;$i -lt $SiteGroups.count;$i++)

    {

    $SITEGROUP = $SiteGroups[$i]

    $site = new-object Microsoft.SharePoint.SPSite ( $SITEURL )

    $web = $site.OpenWeb()

    "Web is : " + $web.Title

    $oSiteGroup = $web.SiteGroups[$SITEGROUP];

    "Site Group is :" + $oSiteGroup.Name

    $oUsers = $oSiteGroup.Users

    foreach ($oUser in $oUsers)

    {

       "Removing user : " + $oUser.Name

       $oSiteGroup.RemoveUser($oUser)

    }

    #Users to be added

    New-SPUser –UserAlias ‘Domain\UserToBeAdded' -group $oSiteGroup –web $web

    }

Page 1 of 1 (3 items)