Forum Discussion

ErnestoAlfaro's avatar
ErnestoAlfaro
Copper Contributor
May 11, 2023

Remove User from all SharePoint groups with SharePoint Online Management Shell

Hi, 
I'm having trouble removing a User from ALL SharePoint groups from my organization.

I've used the example from Microsoft Learn.

 

Connect-SPOService -Url https://contoso-admin.sharepoint.com #Here I changed url to my org admin url
$tenant = "contoso" #Here I changed tenant for my org
$user = "bobbyo" #Here I changed user to user I need to remove
Get-SPOSite | ForEach {Get-SPOSiteGroup –Site $_.Url} | ForEach {Remove-SPOUser -LoginName $user@$tenant.com -Site $_.Url}

 

 But didn't worked, I got a couple of errors...

Then I used this, explained here.

 

#Set parameter values
$AdminSiteURL="https://crescent-admin.sharepoint.com" #here I changed it to my org admin site
$SiteURL="https://crescent.sharepoint.com" #here I changed it to my org site
$LoginName="email address removed for privacy reasons" #here I changed it to the user I need to remove
 
#Get Credentials to connect
$Credentials = Get-Credential
Connect-SPOService -url $AdminSiteURL -credential $Credential
 
#Get all groups 
$Groups= Get-SPOSiteGroup -Site $SiteURL
 
#Remove user from each group
Foreach($Group in $Groups)
{
   Remove-SPOUser -Site $SiteURL -LoginName $LoginName -Group $Group.Name
}

 

 Which didn't returned any errors, but I still see the User in the groups...

What am I doing wrong?
I'll appreciate any help!

Thank you, 
Ernesto Alfaro