SharePoint Online: Remove User from Group using PowerShell
Requirement: Remove User from Group in SharePoint Online using PowerShell.
As an administrator, there may be times when you need to remove a user from a group in SharePoint Online. In this article, we will walk through the steps necessary to do so using the web browser interface and with PowerShell.
In this article:
- SharePoint Online: How to Remove a User from a Group?
- SharePoint Online: Remove User from Group using PowerShell
- Remove All Users from Group in SharePoint Online using PowerShell
- Remove User from Group using SharePoint Online Management Shell
- Remove User from All Groups in a site
- SharePoint Online: Remove a user from a group using PnP PowerShell
SharePoint Online: How to Remove a User from a Group?
Removing users from SharePoint Online security groups can be done in just a few clicks. Follow these steps to delete users from a SharePoint Online group:
- Navigate to your SharePoint Online site where the group is used, Click on Site Settings gear >> Click on Site Settings from the menu.
- From the Site Settings page, click on “People and groups” under the “Users And Permissions” section.
- On the left navigation menu, select the group from which you’d like to remove the users.
- Tick the checkbox next to the user(s) you want to remove.
- Click the Actions drop-down arrow >> Select Remove Users from Group.
- In the confirmation pop-up message box, Click OK to confirm the deletion.
If you don’t see the Actions menu, it’s because you don’t have permission to edit members of that group!
SharePoint Online: Remove User from Group using PowerShell
If you need to remove a user from a SharePoint Online Group, there is no need to go through the Users and Groups page. Instead, you can use PowerShell to quickly and easily remove the user from the group. Let’s use PowerShell to delete users from a SharePoint Online group:
#Load SharePoint CSOM 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"
#sharepoint online remove user from site collection powershell
Function Remove-UserFromGroup()
{
param
(
[Parameter(Mandatory=$true)] [string] $SiteURL,
[Parameter(Mandatory=$true)] [string] $GroupName,
[Parameter(Mandatory=$true)] [string] $UserID
)
Try {
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials
#Get the User
$User=$Ctx.web.EnsureUser($UserID)
$Ctx.Load($User)
#Get the Group
$Group=$Ctx.web.SiteGroups.GetByName($GroupName)
$Ctx.Load($Group)
$Ctx.ExecuteQuery()
#Check if user member of the group
$IsMember = $False
$GroupUsers = $Group.Users
$Ctx.Load($GroupUsers)
$Ctx.ExecuteQuery()
Foreach($GrpUser in $GroupUsers){
if($GrpUser.id -eq $User.Id)
{
$IsMember = $True
}
}
if($IsMember -eq $False)
{
Write-host "User Doesn't Exists in the Group!" -ForegroundColor Yellow
}
else
{
#Remove user from the group
$Group.Users.RemoveByLoginName($User.LoginName)
$Ctx.ExecuteQuery()
Write-host "User Removed from the Group Successfully!" -ForegroundColor Green
}
}
Catch {
write-host -f Red "Error Removing User from Group!" $_.Exception.Message
}
}
#Set parameter values
$SiteURL = "https://crescent.sharepoint.com"
$GroupName="Team Site Members"
$UserID="DavePrudenti@crescent.com"
#Call the function to remove user from group
Remove-UserFromGroup -SiteURL $SiteURL -GroupName $GroupName -UserID $UserID
Remove All Users from Group in SharePoint Online using PowerShell
How about deleting all users from a group? Let’s walk through the process of removing users from a group in SharePoint Online with PowerShell:
#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-AllUserFromGroup()
{
param
(
[Parameter(Mandatory=$true)] [string] $SiteURL,
[Parameter(Mandatory=$true)] [string] $GroupName
)
Try {
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials
#Get the Group
$Group=$Ctx.web.SiteGroups.GetByName($GroupName)
$Ctx.Load($Group)
$Ctx.ExecuteQuery()
#Get users of the group
$GroupUsers = $Group.Users
$Ctx.Load($GroupUsers)
$Ctx.ExecuteQuery()
#Remove all users from the group
ForEach($User in $GroupUsers)
{
$Group.Users.RemoveByLoginName($User.LoginName)
}
$Ctx.ExecuteQuery()
Write-host "All Users are Removed from the Group!" -ForegroundColor Green
}
Catch {
write-host -f Red "Error Removing All Users from Group!" $_.Exception.Message
}
}
#Set parameter values
$SiteURL = "https://crescent.sharepoint.com"
$GroupName="Team Site Members"
#Call the function to remove all users from group
Remove-AllUserFromGroup -SiteURL $SiteURL -GroupName $GroupName
Remove User from Group using SharePoint Online Management Shell
We can also use the Remove-SPOUser cmdlet to remove a user from a specific group.
#Set parameter values
$AdminSiteURL="https://crescent-admin.sharepoint.com"
$SiteURL="https://crescent.sharepoint.com"
$GroupName="Team Site Members"
$LoginName="Salaudeen@crescent.com"
#Get Credentials to connect
$Credentials = Get-Credential
Connect-SPOService -url $AdminSiteURL -credential $Credential
#remove user from sharepoint online group powershell
Remove-SPOUser -LoginName $LoginName -Site $SiteURL -Group $GroupName
Remove User from All Groups in a site
How about deleting a user from all groups of the site? Well, here is how to remove a user from a group in SharePoint Online using PowerShell. First, we will login to our SharePoint Online site using PowerShell. Next, we will get a list of groups of which the user is a member. Finally, we will remove the user from the desired group. Let’s get started!
#Set parameter values
$AdminSiteURL="https://crescent-admin.sharepoint.com"
$SiteURL="https://crescent.sharepoint.com"
$LoginName="Salaudeen@crescent.com"
#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
}
The other methods like CSOM and PnP PowerShell to remove a user from all groups are here: How to Remove a User from All Groups in a SharePoint Online Site using PowerShell?
SharePoint Online: Remove a user from a group using PnP PowerShell
Let’s remove a user from the SharePoint Online group with PnP PowerShell:
#Config Variables
$SiteURL = "https://Crescent.sharepoint.com/sites/marketing"
$GroupName ="Marketing Members"
$UserLoginId = "salaudeen@Crescent.com"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
#Remove user from group
Remove-PnPGroupMember -LoginName $UserLoginId -Identity $GroupName
If you are administering SharePoint Online, there may be times when you need to remove multiple users from a group. You can remove multiple users from the group by wrapping them into an array!
#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/Retail/"
$UserIds = @("salaudeen@crescent.com","steve@crescent.com","AlexW@crescent.com")
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
#Get the Members Group of the site
$MembersGroup = Get-PnPGroup -AssociatedMemberGroup
#Get All members of the Group
$GroupMembers = Get-PnPGroupMember -Identity $MembersGroup | Select -ExpandProperty "Email"
ForEach($UserID in $UserIds)
{
#Check if the user is member of the group
If($GroupMembers.Contains($UserID))
{
#Remove user from group
Remove-PnPGroupMember -LoginName $UserId -Identity $MembersGroup
Write-Host -f Green "User '$UserID' Removed from the Group Successfully!"
}
Else
{
Write-Host -f Yellow "User '$UserID' is not part a member of the Group!"
}
}
Removing users from groups may not prevent users from accessing the sites, if the users are granted direct access such as Full Control, Edit, read, etc., to the site. Here is another article on removing users from the site completely: How to Remove a User from SharePoint Online Site using PowerShell?
Remove-PnPUserFromGroup -LoginName $UserLoginId -Identity $GroupName
The above PnP powershell command doesn’t work straight away. You get “User cannot be found” error. To fix it, use the below format.
Remove-PnPUserFromGroup -LoginName “i:0#.f|membership|$UserLoginId” -Identity $GroupName
With the Latest PnP.PowerShell module:
Remove-PnPGroupMember -LoginName “User@Domain.com” -Identity “Group-Name” also works!
I am having hard time removing “Everyone Except external users” from Shared with me Folder. It is easy to remove it added explicitly on a folder but if added within Sharepoint group, it is little tricky and by default for old users it is added inside Members group.
Nice job putting this all in one place – it had been a couple of years ago that I worked with SharePoint groups – this brought it all back for me. Good Job!!!!
Not able to remove All Users from a Group, showing below error
The SP group has more than 9000 users.
Sorry here is the error
‘Error Removing All Users from Group! Exception calling “ExecuteQuery” with “0” argument(s): “The operation has timed out.”‘