Migrating from Remove-AzureADUser to Remove-MgUser

As Microsoft retires the AzureAD module in favor of the Microsoft Graph PowerShell SDK, it's essential to migrate your user management scripts to the Graph equivalent. One commonly used cmdlet in user offboarding workflows is Remove-AzureADUser, which is now replaced by Remove-MgUser.

In this article, we’ll walk you through how to transition to Remove-MgUser, explain the differences, and provide example use cases — from removing a single user to performing bulk deletions.


What You Did Previously with Remove-AzureADUser

In the AzureAD module, you typically removed users using their UserPrincipalName or ObjectId with a simple command:

Example: Remove a User by UPN

Remove-AzureADUser -ObjectId "john.doe@contoso.com"

While this approach was straightforward, it offered no native support for dry-run previews (-WhatIf) or extended scripting flexibility. Additionally, the AzureAD module is now deprecated and unsupported.


What You Should Do Now with Remove-MgUser

With Microsoft Graph, the recommended cmdlet is Remove-MgUser. It offers a more flexible and secure way to delete users while aligning with modern Graph standards.


Remove a User by UPN

Remove-MgUser -UserId "john.doe@contoso.com"

Remove a User by User ID (GUID)

Remove-MgUser -UserId "12345abc-6789-def0-1234-56789abcdef0"

Remove a User with Confirmation Prompt

Remove-MgUser -UserId "jane.doe@contoso.com" -Confirm

Preview User Removal (Dry Run)

Remove-MgUser -UserId "jane.doe@contoso.com" -WhatIf

This is especially helpful in audit environments or automated scripts where you want to test before applying changes.


Bulk Remove Users from a CSV File

$users = Import-Csv "C:\\Path\\To\\Users.csv"
foreach ($user in $users) {
    Remove-MgUser -UserId $user.UserPrincipalName
}
                                        

CSV Format Example:

UserPrincipalName
john.doe@contoso.com
jane.smith@contoso.com
                                        

What’s Different with Remove-MgUser?


Old (Remove-AzureADUser) New (Remove-MgUser)
Parameter: -ObjectId Parameter: -UserId (can use UPN or GUID)
No -WhatIf support Supports -WhatIf for dry-run deletion preview
Basic deletion only Graph-integrated, supports confirmation & piping
Part of deprecated AzureAD module Part of actively supported Microsoft.Graph.Users
Limited to AzureAD environment Works across the Graph API for broader scenarios

Conclusion

Migrating from Remove-AzureADUser to Remove-MgUser is not just about syntax—it's about adopting a more flexible, extensible, and modern approach to Microsoft 365 user management.
With support for confirmations, previews, and structured scripting, Remove-MgUser is the tool of choice for secure and scalable user offboarding.

Whether you're removing one user or hundreds, this cmdlet fits perfectly into your updated PowerShell automation toolkit.

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