Remove-MgGroupMemberByRef

What is Remove-MgGroupMemberByRef?

Remove-MgGroupMemberByRef is a Microsoft Graph PowerShell cmdlet used to remove an existing member (user, device, or other directory object) from a Microsoft 365 or security group by reference. It deletes the relationship between the group and the specified directory object without deleting the object itself.

🚀 Community Edition Released!

Try the M365Corner Microsoft 365 Reporting Tool — your DIY pack with 20+ out-of-the-box M365 reports for Users, Groups, and Teams.

Why Use Remove-MgGroupMemberByRef?

This cmdlet is essential for automated access cleanup. Administrators can quickly remove users from groups during offboarding, role changes, project closures, or periodic access reviews. It supports scripting, loops, and CSV-based bulk operations, making it far more efficient than manual removal through the admin portals.


Prerequisites

Before using this cmdlet, ensure the following:

  • Microsoft Graph PowerShell module is installed
    Install-Module Microsoft.Graph -Scope CurrentUser
  • You are connected with the required permissions
    Connect-MgGraph -Scopes "Group.ReadWrite.All"

How to use Remove-MgGroupMemberByRef?

Basic syntax:

Remove-MgGroupMemberByRef -GroupId <String> -DirectoryObjectId <String>

You pass the GroupId of the target group and the DirectoryObjectId of the member to be removed. This works for single removals as well as scripted bulk operations using loops or CSV files.


Remove-MgGroupMemberByRef Examples

  • Example 1: Single Removal of a Group Member
  • This example removes one member from a specified group using the group ID and the member’s object ID.

    Remove-MgGroupMemberByRef -GroupId "12345678-9abc-def0-1234-56789abcdef0" -DirectoryObjectId "87654321-fedc-ba98-7654-3210fedcba98"
  • Example 2: Removing Multiple Members from a Group
  • This example loops through multiple member IDs and removes each one from the group.

    $GroupId = "12345678-9abc-def0-1234-56789abcdef0"
    $Members = @("87654321-fedc-ba98-7654-3210fedcba98", "01234567-89ab-cdef-0123-456789abcdef")
    foreach ($Member in $Members) {
        Remove-MgGroupMemberByRef -GroupId $GroupId -DirectoryObjectId $Member
    }
                                                
  • Example 3: Bulk Removal by Reading Data from a CSV File
  • For large-scale operations, members can be removed in bulk by importing their object IDs from a CSV file.

    $GroupId = "12345678-9abc-def0-1234-56789abcdef0"
    $CsvData = Import-Csv -Path "C:\path\to\members.csv"
    foreach ($Member in $CsvData) {
        Remove-MgGroupMemberByRef -GroupId $GroupId -DirectoryObjectId $Member.DirectoryObjectId
    }
                                                

    Sample CSV format:

    DirectoryObjectId
    87654321-fedc-ba98-7654-3210fedcba98
    01234567-89ab-cdef-0123-456789abcdef
                                                

Summary

Key Point Details
Cmdlet Name Remove-MgGroupMemberByRef
Purpose Removes a member from a Microsoft 365 or security group
Required Scope Group.ReadWrite.All
Primary Parameters GroupId, DirectoryObjectId
Automation Benefit Enables bulk group membership cleanup via scripts or CSV
Use Case Offboarding users, access reviews, role or project changes

Did You Know? Managing Microsoft 365 applications is even easier with automation. Try our Graph PowerShell scripts to automate tasks like generating reports, cleaning up inactive Teams, or assigning licenses efficiently.

Ready to get the most out of Microsoft 365 tools? Explore our free Microsoft 365 administration tools to simplify your administrative tasks and boost productivity.

© Your Site Name. All Rights Reserved. Design by HTML Codex