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.

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:

  1. Navigate to your SharePoint Online site where the group is used, Click on Site Settings gear >> Click on Site Settings from the menu.
  2. From the Site Settings page, click on “People and Groups” under the “Users And Permissions” section.
  3. On the left navigation menu, select the group from which you’d like to remove the users.
  4. Tick the checkbox next to the user(s) you want to remove.
  5. Click the Actions drop-down arrow >> Select Remove Users from Group.
  6. In the confirmation pop-up message box, Click OK to confirm the deletion.
    sharepoint online remove user from group

If you don’t see the Actions menu, it’s because you don’t have permission to edit members of that group!

When a user is removed from a SharePoint group, they lose any permissions that were granted to them through membership in that group. If they are not part of any other group with permissions on the site, they will not be able to access the site content they previously had access to.

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 the 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
powershell to remove user from group sharepoint online

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 them from accessing the sites if they are granted direct access, such as Full Control, Edit, Read, etc. Here is another article on removing users from the site completely: How to Remove a User from SharePoint Online Site using PowerShell.

How do I remove Users from the user info list?

To remove a user from the SharePoint User Information List, follow these steps: Navigate to the URL in the browser: https://YourDomain.sharepoint.com/_layouts/15/people.aspx?membershipGroupId=0. This takes you to the “All People” View. Now, you can select and remove users from this User Information List by clicking on “Actions” and then “Delete User from Site Collection.” Alternatively, you can use PowerShell to remove the user from the user information list.

How can I get a list of all SharePoint groups using PowerShell?

To get a list of all SharePoint groups from your SharePoint site, use the following command:
Get-SPOSiteGroup -Site https://your-domain.sharepoint.com/sites/your-site | Where {$_.LoginName -notlike "sharingLinks" -and $_.LoginName -notlike "Limited Access"}

Will the removed user be notified when they are removed from the group?

No, SharePoint Online does not automatically notify users when they are removed from a group.

How do I know which group a user is in?

To find out which groups a user is a part of, go to the site permissions and then click on “Advanced permissions settings.” Here, you can use the “Check Permissions” feature and enter the user’s name to see their permission levels and group memberships.

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

6 thoughts on “SharePoint Online: Remove User from Group 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

    Reply
    • With the Latest PnP.PowerShell module:
      Remove-PnPGroupMember -LoginName “User@Domain.com” -Identity “Group-Name” also works!

      Reply
  • 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.

    Reply
  • 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!!!!

    Reply
  • Not able to remove All Users from a Group, showing below error

    The SP group has more than 9000 users.

    Reply
    • Sorry here is the error
      ‘Error Removing All Users from Group! Exception calling “ExecuteQuery” with “0” argument(s): “The operation has timed out.”‘

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *