kwizcom banner advertisement

Remove User from SharePoint Group using PowerShell

Remove user from SharePoint group:
From SharePoint Web interface, if you want to remove user from SharePoint group, Login as a SharePoint Administrator, navigate to:
  • Click on Site Settings Gear icon >> Click on Site Settings
  • From the Site settings page, Click on "People and Groups" link under "Users and Permissions" group
    sharepoint powershell remove user from group
  • Select the group from the left navigation by Click on the group from which you want to remove users.
    sharepoint 2013 remove user from group programmatically
  • Select the user(s) and click on Actions Menu >> Choose "Remove users from Group"
    remove user from sharepoint group using powershell
Confirm by clicking "OK". Selected users are now removed from the SharePoint Group.

Delete user from SharePoint group using PowerShell: 
If you want to remove user from SharePoint group, this PowerShell script can help:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

Function RemoveUser-FromGroup($SiteURL, $GroupName, $UserAccount)
{
 try
    {
     $ErrorActionPreference = "Stop"
  
     #Get the Web
     $web=Get-SPWeb $SiteURL
    
     #Get the User to Remove 
     $User  = Get-SPUser -Identity $UserAccount -Web $web

     #Get the Group by its name
     $Group = $Web.sitegroups | Where-Object {$_.Name -eq $GroupName}
     if($Group -ne $null)
     {
         #sharepoint powershell delete user from group
         $Group.RemoveUser($User)
         Write-Host "$($User) Removed from the Group: $($GroupName)"
     }
   }
 catch
    { 
           #Write error message on screen and to a LOG file
           write-host $_.Exception.Message
    }
 finally
    {
           $ErrorActionPreference = "Continue"
    }
}

#Call the function to remove user from SharePoint group
RemoveUser-FromGroup "http://sharepoint.crescent.com/sites/Operations" "Operations Members" "Corp\DaveP"
This removes given user from specific SharePoint group. How about removing user from All groups in the site?

Remove user from all SharePoint groups
Lets remove user from All Groups in SharePoint using PowerShell
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

Function RemoveUser-FromAllGroups($SiteURL, $UserAccount)
{
     #Get the Web
     $web=Get-SPWeb $SiteURL
    
     #Get the User to Remove 
     $User  = $Web.EnsureUser($UserAccount)

     #Iterate through all Groups
     foreach($Group in $Web.Groups)
     {
         $GroupUser = $Group.Users | where {$_.UserLogin -eq $User.UserLogin}
         #Check if user member of the group
         if($GroupUser -ne $null)
         {
            #remove user from sharepoint group using powershell 
            $Group.RemoveUser($User)
            Write-Host "$($User) Removed from the Group: $($Group)"
         }
     }
 }

 #Call the function to remove user from all groups in the site  
 RemoveUser-FromAllGroups "http://intranet.crescent.com" "Crescent\Salaudeen"

Web.Group vs Web.SiteGroup: 
Web.SiteGroups gets you a collection of cross-site groups for the site collection. These groups can be used in more than one Web object in the site collection.

Web.Groups gets you the groups that has some permission on the specific Web (subsite). These groups may have permission somewhere in the site. E.g. List or List Item with broken permissions. So if you add group to a site without any permission on the site, then this group won't appear in Web.Groups collection, but it will appear in Web.SiteGroups collection.
Remove User from SharePoint Group using PowerShell Remove User from SharePoint Group using PowerShell Reviewed by Salaudeen Rajack on 4:48 PM Rating: 5

3 comments:

  1. how about deleting multiple users from the sharepoint group

    ReplyDelete
    Replies
    1. Sure, You can either use the function "RemoveUser-FromAllGroups" repeatedly with different user accounts, to remove them from all groups or "RemoveUser-FromGroup" function with specific group names.

      Delete
  2. on the groups deleting a user from all groups

    this line: foreach($Group in $Web.Groups)

    used this instead to cater for all: foreach($Group in $Web.SiteGroups)

    This is on sharepoint 2016

    ReplyDelete

Please Login and comment to get your questions answered!

Powered by Blogger.