How to Use Remove-MgUser to Delete Microsoft 365 Users?

Managing users is a critical task for any Microsoft 365 administrator, and sometimes that includes removing users who have left the organization or no longer need access. Microsoft Graph PowerShell makes this easy with the Remove-MgUser cmdlet.

In this article, we’ll walk through everything you need to know about Remove-MgUser, including usage scenarios, syntax, and real-world examples.


What is Remove-MgUser?

Remove-MgUser is a Microsoft Graph PowerShell cmdlet that allows administrators to delete a user from Microsoft 365/Azure Active Directory. It works by identifying the user via their UserPrincipalName (email) or Object ID (GUID), then permanently removing them from the tenant.


Why Use Remove-MgUser?

Using New-MgGroupOwner offers many benefits:

  • Deprovision former employees
  • Clean up test or inactive accounts
  • Automate user offboarding processes
  • Ensure compliance with security and licensing policies

This cmdlet is particularly useful for scripting and automation scenarios, especially when used in combination with user reports or CSV imports.


Cmdlet Syntax

Remove-MgUser -UserId <String> [-Confirm] [-WhatIf]

Parameters:

  • -UserId: Required. The UserPrincipalName (e.g., john@domain.com) or Object ID (GUID) of the user.
  • -Confirm: Optional. Prompts for confirmation before deleting the user.
  • -WhatIf: Optional. Simulates the deletion without actually performing it.

Usage Examples

Example 1: Remove a User by UPN

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

This deletes the user with the specified User Principal Name (UPN).

Example 2: Remove a User by User ID

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

Useful when you're working with object IDs from reports or API responses.

Example 3: Remove a User with Confirmation

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

Adds a confirmation prompt before deleting the user—great for manual control.

Example 4: Preview User Removal

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

This simulates the deletion and shows what would happen—helpful for testing scripts safely.

Example 5: 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:

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

This is perfect for offboarding multiple users at once using a pre-generated CSV.


Frequently Asked Questions

Can I undo a user deletion?

Not directly. Once a user is deleted, they move to the deleted users section in Azure AD and can be restored within 30 days, depending on your retention settings.

What happens to the user’s mailbox and data?

Unless retention or litigation hold policies are applied, the mailbox and OneDrive data will also be deleted after the retention period.

Can I use this cmdlet on guest users?

Yes, as long as you have their UserId, you can remove guest users the same way.


Use Cases

  • Employee Offboarding: Automate user removal as part of HR workflows.
  • Test Environment Cleanup: Remove dummy accounts post-testing.
  • Security Audit: Remove stale or orphaned accounts detected during audits.

Conclusion

The Remove-MgUser cmdlet is an essential tool for every Microsoft 365 administrator. Whether you're cleaning up users manually or building automated deprovisioning pipelines, this cmdlet gives you the power and flexibility to manage user lifecycles efficiently.


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