The solutions below require only SharePoint Online Management Shell.



The article outlines several scenarios for removing SharePoint users from site groups in SharePoint sites and offers quick PowerShell solutions for each scenario.

Prerequisites


1. You have installed SharePoint Online Management Shell and opened it as Administrator.
2. You have run the following cmdlet to connect to SharePoint Service:

Remove one user from all site groups


$user="Anita.kowalska@mycompany.com"
 
$groups=Get-SPOSiteGroup -Site $site
 
foreach ($group in $groups){Remove-SPOUser -Site $site -LoginName $user -Group $group.LoginName}

Remove one user from various site groups 


Define groups


First you need to define what groups you want. You can either use Where {} clause:
$groups=Get-SPOSiteGroup -Site $site | where {$_.Title -match "Teams"}

or you can import them from a CSV file:
 
$groups=import-csv c:\groups.csv

Verify that the groups are what you wanted:
$groups



Remove


And run the rest of the cmdlets:
$user="Anita.kowalska@mycompany.com"
  
foreach ($group in $groups){Remove-SPOUser -Site $site -LoginName $user -Group $group.LoginName}

Remove multiple users from all site groups


First, you need to define what users you'd like to remove. You can define them as an array or import from a CSV file:
$users=("Anita.kowalska@mycompany.com", "Michal.Misiecki@mycompany.com")

$users=(import-csv c:\users.csv).Loginname

$site=https://MyCompany.Sharepoint.com/sites/site1
$groups=Get-SPOSiteGroup -Site $site
foreach($user in $users) { foreach ($group in $groups){Remove-SPOUser -Site $site -LoginName $user -Group $group.LoginName}}

You can also remove users based on some criteria they fulfill, e.g.
 
$groups=Get-SPOSiteGroup -Site $site
$users=get-spouser -Site $site
 
foreach($user in $users) {if($user.LoginName -match "Team"){ foreach ($group in $groups){Remove-SPOUser -Site $site -LoginName $user.loginName -Group $group.LoginName}}}

Remove chosen users from chosen site groups in multiple site collections


 
foreach($site in $sites)
{
$groups=Get-SPOSiteGroup -Site $site | where {$_.Title -match "Teams"}
$users=get-spouser -Site $site
 
foreach($user in $users) {if($user.LoginName -match "TeamMember"){ foreach ($group in $groups){Remove-SPOUser -Site $site -LoginName $user.loginName -Group $group.LoginName}}}
 
}

Other Languages

The article is available in other languages:
SharePoint Online: Usuwanie użytkowników z grup witryny SharePoint za pomocą programu Powershell (pl-PL)