Sunday, October 18, 2015

Remove User from SharePoint Groups 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
  • Pick the appropriate group from the left navigation
    sharepoint 2013 remove user from group programmatically
  • Select the user and click on Actions >> Choose "Remove users from Group"
    remove user from sharepoint group using powershell
Delete user from SharePoint group using PowerShell: 
If you want to remove user from group programmatically, 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)
     {
         $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 will remove 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)
         {
            $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"



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

No comments :

Post a Comment

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...