Welcome to deBUG.to Community where you can ask questions and receive answers from Microsoft MVPs and other experts in our community.
1 like 0 dislike
11 views
ago in PowerShell by 20 34 41

I am managing a system that includes 50+ subsites, and I need to remove a particular user's permission from all of these subsites. How to efficiently remove a user from all SharePoint groups using PowerShell script? instead of having to manually loop through each site permission.


1 Answer

1 like 0 dislike
ago by 20 34 41

To remove user from all SharePoint groups using PowerShell

The following PowerShell script enables you to remove user's permission from all the SharePoint groups in each subsite in seconds

Add-PSSnapin Microsoft.SharePoint.PowerShell 
$username = "UserX"
$siteCollection = Get-SPSite "https://rootSite/debug"

Function RemoveUserFromGroup($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)
         {

            Write-Host "$($User) is member of Group: $($Group)"

            #remove user from sharepoint group using powershell 
            $Group.RemoveUser($User)
            Write-Host "$($User) Removed from the Group: $($Group)"
           
         }
     }
 }

foreach ($site in $siteCollection.AllWebs){

#Call the function to remove user from all groups in the site  
 RemoveUserFromGroup $site $username 

}
If you don’t ask, the answer is always NO!
...