Migrate from Remove-AzureADGroup to Remove-MgGroup

As Microsoft deprecates the AzureAD module in favor of the Microsoft Graph PowerShell SDK, administrators must transition their scripts and automation tools. One commonly used cmdlet for removing Microsoft 365 groups is Remove-AzureADGroup.

This article will guide you through migrating to the modern Remove-MgGroup cmdlet and how to use it for various scenarios like single deletion, bulk removal, conditional deletion, and dry-run previews.


What You Did Previously with Remove-AzureADGroup?

The AzureAD module allowed administrators to remove Microsoft 365 groups using their group object IDs.

Example

Remove-AzureADGroup -ObjectId "12345678-90ab-cdef-1234-567890abcdef"

This command removed the specified group without asking for confirmation (unless specified).

What You Should Do Now with Remove-MgGroup?

The Graph PowerShell equivalent is Remove-MgGroup, which offers the same functionality, but with better extensibility and alignment to the Microsoft Graph API.

Example 1: Remove Group by ID

Remove-MgGroup -GroupId "12345678-90ab-cdef-1234-567890abcdef"

Example 2: Remove a Group with Confirmation

Remove-MgGroup -GroupId "12345678-90ab-cdef-1234-567890abcdef" -Confirm

Example 3: Bulk Remove Groups from a CSV File

CSV File Format

GroupId
12345678-90ab-cdef-1234-567890abcdef
abcdef12-3456-7890-abcd-ef1234567890
$groups = Import-Csv -Path "C:\path\to\groups.csv"
foreach ($group in $groups) {
    Remove-MgGroup -GroupId $group.GroupId 
}

This script removes all groups listed in the CSV file without prompting for confirmation.

Example 4: Removing Groups with Specific Criteria

Let’s say you want to remove all groups that have zero members:

$groups = Get-MgGroup -Filter "members/$count eq 0"
foreach ($group in $groups) {
    Remove-MgGroup -GroupId $group.Id 
}

Example 5: Preview Group Removal (Safe Execution)

Remove-MgGroup -GroupId "12345678-90ab-cdef-1234-567890abcdef" -WhatIf

This preview shows what would happen if you ran the removal, without executing it — great for validation.


What’s Different with Remove-MgGroup?


Legacy Cmdlet Modern Cmdlet
Remove-AzureADGroup Remove-MgGroup
Uses -ObjectId Uses -GroupId
AzureAD module Microsoft.Graph.Groups
No support for -WhatIf Supports -WhatIf, -Confirm, and more
Less extensible Fully extensible via Graph

Tip: Always test with -WhatIf before running deletion scripts, especially in bulk.


Conclusion

Migrating from Remove-AzureADGroup to Remove-MgGroup is straightforward, and the benefits are significant. With modern security, better extensibility, and strong scripting support, Graph PowerShell ensures you stay compliant and future-ready.

Make the shift today to future-proof your scripts and embrace the power of Microsoft Graph. Visit M365Corner.com for ready-to-use free Microsoft Graph PowerShell tools and step-by-step migration guides built for Microsoft 365 administrators.



Permission Required

Example:


                                


                                


                                

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