SharePoint Online: Remove User from All Groups in a Site using PowerShell

Requirement: Remove user from all groups in a SharePoint Online Site Collection

SharePoint Online: PowerShell to Remove User from All Groups
This PowerShell script gets all groups of the given user and removes from the group.
#Set Parameters
$AdminCenterURL="https://crescent-admin.sharepoint.com"
$SiteUrl = "https://crescent.sharepoint.com/sites/marketing"
$UserLoginID = "i:0#.f|membership|salaudeen@crescent.com"

#Connect to SharePoint Online
Connect-SPOService -Url $AdminCenterURL -credential (Get-Credential)

#Get All Groups of the User
$UserGroups = Get-SPOUser -LoginName $UserLoginID -Site $SiteURL | Select -ExpandProperty Groups

#Remove User from all Groups
$UserGroups | ForEach-Object { Remove-SPOUser -Group $_ -Site $SiteUrl -LoginName $UserLoginID}
Run this script in SharePoint Online Management Shell.

Remove User from All Groups using PowerShell CSOM
#Load SharePoint Online Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
 
#Function to remove all users from a group
Function Remove-UserFromAllGroups()
{
  param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $UserID
    )
   Try {
        $Cred= Get-Credential
  
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
          
        #Get all Groups from the site
        $Groups=$Ctx.web.SiteGroups
        $Ctx.Load($Groups)
        $Ctx.ExecuteQuery()

        #Iterate through Each Group
        Foreach($Group in $Groups)
        {
            #Get all users from the group
            $Ctx.Load($Group.Users)
            $Ctx.ExecuteQuery()

            ForEach($User in $Group.Users)
            {
                If($User.LoginName -eq $UserID)
                {
                    $Group.Users.RemoveByLoginName($User.LoginName)
                    Write-host "User Removed from Group:"$Group.Title  -ForegroundColor Green        
                }
            }
            $Ctx.ExecuteQuery()
        }        
    }
    Catch {
        write-host -f Red "Error Removing All Users from Group!" $_.Exception.Message
    }
} 

#Set parameter values
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$UserID= "i:0#.f|membership|salaudeen@crescent.com"
 
#Call the function to remove user from all groups
Remove-UserFromAllGroups -SiteURL $SiteURL -UserID $UserID

PnP PowerShell to Remove User from All Groups in a Site
#Set Variables
$SiteURL = "https://crescent.sharepoint.com/sites/Marketing"
$UserLoginID = "i:0#.f|membership|salaudeen@crescent.com"
 
#Connect to PNP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Get All Groups of the User
$UserGroups = Get-PnPUser -Identity $UserLoginID | Select -ExpandProperty Groups

#Remove User from all Groups
$UserGroups | ForEach-Object { Remove-PnPUserFromGroup -LoginName $UserLoginID -Identity $_.Title } 

SharePoint Online: Remove User from All Groups in a Site using PowerShell SharePoint Online: Remove User from All Groups in a Site using PowerShell Reviewed by Salaudeen Rajack on January 07, 2019 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.