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
}