SharePoint Online: Remove User from Group using PowerShell
Requirement: Remove User from Group in SharePoint Online using PowerShell
SharePoint Online: How to Remove a User from Group?
Removing users from SharePoint Online security groups can be done in just few clicks. Follow these steps to delete users from a SharePoint Online group:
SharePoint Online: Remove User from Group using PowerShell
Lets use PowerShell to delete users from a SharePoint online group.
Remove All Users from Group in SharePoint Online using PowerShell
How about deleting all users from a group?
Remove User from Group using SharePoint Online Management Shell
We can also use Remove-SPOUser cmdlet to remove a user from specific group.
Remove User from All Groups in a site:
How about deleting a user from all groups of the site?
SharePoint Online: Remove user from group using PnP PowerShell
Let's remove a user from SharePoint Online group with PnP PowerShell:
SharePoint Online: How to Remove a User from Group?
Removing users from SharePoint Online security groups can be done in just 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 Users And Permissions section.
- On the Left navigation menu, select the group from which you'd like to remove the users.
- Tick the check box 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.
SharePoint Online: Remove User from Group using PowerShell
Lets 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" 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="[email protected]" #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?
#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 Remove-SPOUser cmdlet to remove a user from specific group.
#Set parameter values $AdminSiteURL="https://crescent-admin.sharepoint.com" $SiteURL="https://crescent.sharepoint.com" $GroupName="Team Site Members" $LoginName="[email protected]" #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?
#Set parameter values $AdminSiteURL="https://crescent-admin.sharepoint.com" $SiteURL="https://crescent.sharepoint.com" $GroupName="Team Site Members" $LoginName="[email protected]" #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 }
SharePoint Online: Remove user from group using PnP PowerShell
Let's remove a user from SharePoint Online group with PnP PowerShell:
#Config Variables $SiteURL = "https://crescenttech.sharepoint.com/sites/marketing" $GroupName ="Marketing Members" $UserLoginId = "[email protected]" #Get Credentials to connect $Cred = Get-Credential #Connect to PNP Online Connect-PnPOnline -Url $SiteURL -Credentials $Cred #Remove user from group Remove-PnPUserFromGroup -LoginName $UserLoginId -Identity $GroupName
Not able to remove All Users from a Group, showing below error
ReplyDeleteThe SP group has more than 9000 users.
Sorry here is the error
Delete'Error Removing All Users from Group! Exception calling "ExecuteQuery" with "0" argument(s): "The operation has timed out."'
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!!!!
ReplyDeleteI 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.
ReplyDelete