How to Remove a User from Office 365 Group using PowerShell?

Requirement: Remove a user from Office 365 group using PowerShell.

How to remove a User from Office 365 Group?

Microsoft 365 groups are an essential tool for collaboration and communication within organizations. If you are an Office 365 administrator, there may come a time when you need to remove a user from the group. This article will show you how to remove a user from an Office 365 group using PowerShell and the Azure Active Directory Module for PowerShell. We will also show you how to remove a user from an Office 365 group using the Microsoft 365 Admin Center.

Remove User from Microsoft 365 Group using Microsoft 365 admin center

You can remove members from any Office 365 group through Microsoft 365 admin center as an admin. Here is how:

  1. Log in to the Microsoft 365 Admin Center site: https://admin.microsoft.com
  2. Expand “Teams & Groups” and Click on “Active Teams & Groups” in the left navigation.
  3. Search and Select the Office 365 group you wish to remove members.
  4. On the Group Details page, click on the “Members” tab >>Select the users you want to remove.
  5. Now, you can remove group members by clicking on the “Remove as member” button next to each member. You can also select multiple users and remove them in one click. Similarly, you can click on the “Owners” tab to remove a group owner.
    remove members from office 365 group
  6. Hit close once you are done!

How to Remove Group Memberships of a Microsoft 365 User?

Instead of going through Groups, You can proceed through the specific user and remove the group membership. Here is how:

  1. Open the Admin app in Office 365 >> In the left navigation pane, select Users >> Active users.
  2. Search for the user you want to remove, select the three dots from the user object, and then select “Manage groups”.
  3. In the next window, Select the Group(s) you would like to remove and click on the “Remove” button and confirm the prompt.remove user from microsoft 365 group

PowerShell to Remove User from Office 365 Group

A quick and easy way to remove a user from an Office 365 group is to use Azure AD PowerShell. Here is how we can use PowerShell to remove a user from Office 365 group:

#Variables
$GroupName = "IT Team"
$UserUPN = "salaudeen@crescent.com"

#Connect to AzureAD
Connect-AzureAD -Credential (Get-Credential)
 
#Get the Azure AD Group
$AADGroup = Get-AzureADGroup -Filter "DisplayName eq '$GroupName'"

#Get the Azure AD User
$AADUser  = Get-AzureADUser -Filter "UserPrincipalName eq '$UserUPN'"

#Remove User from Group
Remove-AzureADGroupMember -ObjectId $AADGroup.ObjectID  -MemberId $AADUser.ObjectID 

Similarly, You can remove a user from all Office 365 Groups quickly through PowerShell as:

#Variables
$UserUPN = "salaudeen@crescent.com"

#Connect to AzureAD
Connect-AzureAD -Credential (Get-Credential) | Out-Null
 
#Get all Azure AD Unified Groups
$AADGroups = Get-AzureADMSGroup -Filter "groupTypes/any(c:c eq 'Unified')" -All:$true

#Get the Azure AD User
$AADUser  = Get-AzureADUser -Filter "UserPrincipalName eq '$UserUPN'"

#Check each group for the user
ForEach ($Group in $AADGroups) 
{
    $GroupMembers = (Get-AzureADGroupMember -ObjectId $Group.id).UserPrincipalName
    If ($GroupMembers -contains $UserUPN)
    {
        #Remove user from Group
        Remove-AzureADGroupMember -ObjectId $Group.Id -MemberId $AADUser.ObjectId 
        Write-Output "$UserUPN was removed from $($Group.DisplayName)"
    }
}

How about removing multiple users in Microsoft 365 groups from a CSV file? My CSV file has a “UPN” column with the login IDs of users.

#Variables
$CSVFile = "C:\Temp\UserList.csv"
 
#Connect to AzureAD
Connect-AzureAD -Credential (Get-Credential) | Out-Null
  
#Get all Azure AD Unified Groups
$AADGroups = Get-AzureADMSGroup -Filter "groupTypes/any(c:c eq 'Unified')" -All:$true

#Iterate through each line in CSV
Import-CSV $CSVFile | ForEach-Object {
    #Get the UPN
    $UPN = $_.UPN
    
    #Get the Azure AD User
    $AADUser  = Get-AzureADUser -Filter "UserPrincipalName eq '$UPN'"

    #Check each group for the user
    ForEach ($Group in $AADGroups) 
    {
        $GroupMembers = (Get-AzureADGroupMember -ObjectId $Group.id).UserPrincipalName
        If ($GroupMembers -contains $UPN)
        {
            #Remove user from Group
            Remove-AzureADGroupMember -ObjectId $Group.Id -MemberId $AADUser.ObjectId
            Write-Output "$UPN is removed from Group '$($Group.DisplayName)'"
        }
    }
}

Remove Office 365 Group Member with Exchange Online PowerShell

We can delete a Microsoft 365 group member with Exchange Online PowerShell as well:

#Connect to Exchange Online
Connect-ExchangeOnline -Credential (Get-Credential) -ShowBanner:$false
 
#PowerShell to remove a Member from office 365 group
Remove-UnifiedGrouplinks -Identity "Procurement@Crescent.com" -LinkType "Members" -Links "Steve@Crescent.com"

PnP PowerShell to Remove a Member from Office 365 Group

Use the Remove-PnPMicrosoft365GroupMember cmdlet to remove a member from Microsoft 365 group.

#Config Variables
$AdminSiteURL = "https://crescent-admin.sharepoint.com"
$GroupEmail = "HRAdmin@Crescent.com"
$MemberToRemove = "Alex@Crescent.com"

Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $AdminSiteURL -Interactive

    #Get the Office 365 Group from Email
    $Group = Get-PnPMicrosoft365Group | Where Mail -eq $GroupEmail

    #Check if group exists
    If($Group -ne $Null)
    {
        #Get Group Members
        $GroupMembers = Get-PnPMicrosoft365GroupMembers -Identity $Group | Select -ExpandProperty Email

        If($GroupMembers -contains $MemberToRemove)
        {
            #Remove Member from Office 365 group
            Remove-PnPMicrosoft365GroupMember -Identity $Group -Users $MemberToRemove
            Write-Host "Member removed from the Group Successfully!" -f Green

        }
        Else
        {
            Write-Host "Could not find Member in the Group!" -f Yellow
        }
    }
    Else
    {
        Write-host "Could not Find Group!" -f Yellow
    }
}
Catch {
    write-host -f Red "Error:" $_.Exception.Message
}

If you want to remove all members from a Microsoft 365 group, use the cmdlet: Clear-PnPMicrosoft365GroupMember.

Similarly, you can remove the owner of the group using the Remove-PnPMicrosoft365GroupOwner cmdlet:

#Config Variables
$AdminSiteURL = "https://salaudeen-admin.sharepoint.com"
$GroupEmail = "HRAdmin@lsqp.onmicrosoft.com"
$OwnersToRemove = "Alexw@lsqp.onmicrosoft.com"

Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $AdminSiteURL -Interactive

    #Get the Office 365 Group from Email
    $Group = Get-PnPMicrosoft365Group | Where Mail -eq $GroupEmail

    #Check if group exists
    If($Group -ne $Null)
    {
        #Get Group Owners
        $GroupOwners = Get-PnPMicrosoft365GroupOwner -Identity $Group | Select -ExpandProperty Email

        If($GroupOwners -contains $OwnersToRemove)
        {
            #Remove Owner from Office 365 group
            Remove-PnPMicrosoft365GroupOwner -Identity $Group -Users $OwnersToRemove
            Write-Host "Owner removed from the Group Successfully!" -f Green

        }
        Else
        {
            Write-Host "Could not find Owner in the Group!" -f Yellow
        }
    }
    Else
    {
        Write-host "Could not Find Group!" -f Yellow
    }
}
Catch {
    write-host -f Red "Error:" $_.Exception.Message
}

PnP PowerShell to Remove User from a SharePoint Online Site’s Associated Group

When a site is connected with Microsoft 365 group, permissions can be managed at the group level. E.g., To remove a user from the site, You have to remove him from the group.

#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/CorporateBranding"
$UserId= "Steve@crescent.onmicrosoft.com"
 
Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Interactive

    #Get the Site
    $Site = Get-PnPSite -Includes GroupId

    #Remove user from the Site's associated Microsoft 365 Group
    Remove-PnPMicrosoft365GroupMember -Identity $Site.GroupId -Users $UserId
    Write-host "Removed User from the Associated Microsoft 365 Group!" -f Green    
}
Catch {
    Write-host -f Red "Error:" $_.Exception.Message
}

Similarly, to remove the user from the Group owners, use the following:

Remove-PnPMicrosoft365GroupOwner -Identity $Site.GroupId -Users $UserId

Wrapping up

In conclusion, removing a user from a Microsoft 365 group is a straightforward process that can be done using the Microsoft 365 admin center or using PowerShell. By removing users who are no longer a part of the organization or no longer need access to the group’s resources, administrators can ensure the security and privacy of the information contained in the group.

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!

10 thoughts on “How to Remove a User from Office 365 Group using PowerShell?

  • Hello All

    The code for removing the user from all the M365 groups on the tenant (2nd code snippet from the top of the page) works only in the event if the user to be removed is a Member of a group, if the user is the Owner, the removal won’t be done. I have modified Salaudeen’s Foreach loop in that code snippet to address this issue and from the testing that I have done, this updated code removes the user from the group regardless if they’re a member, owner, or both. Please find the updated Foreach code below.

    ForEach ($Group in $AADGroups)
    {
    $GroupMembers = (Get-AzureADGroupMember -ObjectId $Group.id).UserPrincipalName
    $GroupOwners = (Get-AzureADGroupOwner -ObjectId $Group.id).UserPrincipalName
    If ($GroupMembers -contains $UserUPN)
    {
    #Remove user from Group
    Remove-AzureADGroupMember -ObjectId $Group.Id -MemberId $AADUser.ObjectId
    Write-Output “$UserUPN was removed from $($Group.DisplayName)”
    }
    If($GroupOWners -contains $UserUPN)
    {
    Remove-AzureADGroupOwner -ObjectId $Group.Id -OwnerId $AADUser.ObjectId
    Write-Output “$UserUPN was removed from $($Group.DisplayName)”
    }
    }

    Since I’m not a PS expert, I’m not sure whether this follows the best approach in terms of coding, but it definitely works so hopefully someone else will find it useful 🙂 nevertheless, thanks Salaudeen for providing the code sample and pointing me in the right direction.

    Reply
  • Hi Salaudeen. Thanks as always for showing us how to do these things. I have a question about removing a big list of users (in a CSV file), from a single MS 365 group (rather than from all groups).

    Reply
  • I was looking for an script which would remove the users from any M365 groups, DL & Mail-enabled security group. similar to the one shared here

    Reply
  • Hi, that’s very usefull script but i want to delete users also from distribution list. So that i removed ‘-Filter “grou(..)’ from $AADGroups. I thought it works as I got message “some@emial is removed from Group ‘somegroup’ ” but there is error.
    “Message: Cannot Update a mail-enabled security groups and or distribution list.”

    Probably the solution would be Remove-DistributionGroupMember instead of Remove-AzureADGroupMember. But how to filter distribution groups?
    Can you help ?

    Reply
  • How was the CSV created that worked with the bulk script?

    Reply
  • This is a big help. Is there a way to exclude a single group. My hope is to remove the user from all groups with one exception to which they will remain a member (if they were a member). Unfortunately, my automation does not work if they are removed and added back to this group.

    Reply
    • You can just wrap the removal code in an If statement that excludes that group.

      if($Group.DisplayName -ne “Group to keep them in”)
      {
      #do the deletions as normal
      }

      Reply
  • This is helpful for 1 user. What script can I run if I wanna remove a number of users saved in a CSV file? I want to remove all those users from all groups. It’s over 900 users by the way.

    Reply
    • Sure, Article has been updated to bulk remove users in Microsoft 365 Groups from a CSV file.

      Reply

Leave a Reply

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