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

Requirement: Remove user from office 365 group using PowerShell

How to remove a User from Office 365 Group?

As an admin, you can remove members from any Office 365 group through Microsoft 365 admin center. Here is how:

  • Login to the Microsoft 365 Admin Center site: https://admin.microsoft.com
  • Expand Groups and Click on the Groups link in the left navigation. 
  • Search and Select the Office 365 group you wish to remove members.
  • In the Group Details page, click on the “Members” tab >> Click on View all and manage members link. 
    remove user from office 365 group
  • Now you can remove group members by clicking on the “X” button next to each member. Hit save once you are done!
    remove user from office 365 group powershell

PowerShell to Remove User from Office 365 Group

Here is how we can use PowerShell to remove a user from Office 365 group:

#Variables
$GroupName = "IT Team"
$UserUPN = "[email protected]"

#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

#Variables
$UserUPN = "[email protected]"

#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 “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)'"
        }
    }
}

Salaudeen Rajack

Information Technology Professional with Two decades of SharePoint Experience.

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

  • February 9, 2021 at 4:16 PM

    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
  • February 9, 2021 at 7:28 AM

    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
  • August 24, 2020 at 10:45 PM

    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
    • August 25, 2020 at 2:18 PM

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

      Reply
      • June 10, 2021 at 7:29 AM

        You can just wrap the removal code in an If statement that excludes that group.
        if($Group.DisplayName -neq “Group to keep them in”){
        #do the deletions as normal
        }

        Reply

Leave a Reply