Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
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
- You have installed SharePoint Online Management Shell and opened it as Administrator.
- You have run the following cmdlet to connect to SharePoint Service:
Connect-SPOService "https://MyCompany-admin.sharepoint.com"
Remove one user from all site groups
$site="https://MyCompany.Sharepoint.com/sites/site1"
$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:
$site="https://MyCompany.Sharepoint.com/sites/site1"
$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.
$site="https://MyCompany.Sharepoint.com/sites/site1"
$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
$sites=("https://MyCompany.Sharepoint.com/sites/site1","https://MyCompany.Sharepoint.com/sites/site2","https://MyCompany.Sharepoint.com/sites/site3")
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)