Simplify user tasks like bulk creation, updates, password resets, deletions, license checks & more — all from one place.
🚀 Launch ToolkitRemoving inactive or unnecessary user accounts from Microsoft 365 is just as important as creating them. With Microsoft Graph PowerShell, the Remove-MgUser cmdlet allows administrators to delete user accounts securely and efficiently — whether individually, with confirmation, or in bulk.
In this article, you’ll explore five simple PowerShell scripts that use Remove-MgUser to manage user deletions safely. Each script is practical, easy to follow, and designed for beginners who want to automate user cleanup tasks.
Before running these scripts, make sure you’ve installed and connected to Microsoft Graph PowerShell:
Install-Module Microsoft.Graph -Scope CurrentUser
Connect-MgGraph -Scopes "User.ReadWrite.All"
Once connected, you can use the following scripts to remove users from your tenant.
This method uses the UserPrincipalName (UPN) of the user you want to delete.
Remove-MgUser -UserId "john.doe@contoso.com"
Explanation:
This script deletes the user account with the specified UPN — in this case, john.doe@contoso.com. It’s one of the simplest and most direct ways to remove a user. Make sure you use the correct UPN to avoid accidentally deleting the wrong account.
Sometimes you may only have the User ID (GUID) instead of the UPN. You can still remove the user easily.
Remove-MgUser -UserId "12345abc-6789-def0-1234-56789abcdef0"
Explanation:
Here, the user is deleted using their unique object ID. This is useful when working with scripts or APIs that return GUIDs instead of email addresses.
It’s always good to confirm before performing irreversible actions. The -Confirm parameter prompts you before deletion.
Remove-MgUser -UserId "jane.doe@contoso.com" -Confirm
Explanation:
When you run this command, PowerShell asks for confirmation before proceeding with the deletion. This prevents accidental removal of user accounts, making it a safer option for manual administration.
If you want to see what would happen without actually deleting the user, use the -WhatIf parameter.
Remove-MgUser -UserId "jane.doe@contoso.com" -WhatIf
Explanation:
This script performs a “dry run” of the deletion process, showing what would occur if you executed the command for real. It’s an excellent way to validate scripts before applying them to production environments.
If you need to delete multiple users at once, you can automate the process with a CSV file.
$users = Import-Csv "C:\Path\To\Users.csv"
foreach ($user in $users) {
Remove-MgUser -UserId $user.UserPrincipalName
}
CSV File Structure
Your CSV file should contain the following column header:
UserPrincipalName
john.doe@contoso.com
jane.doe@contoso.com
mark.johnson@contoso.com
Explanation:
This script imports all user UPNs from the CSV file and deletes them one by one. It’s a great way to clean up multiple accounts efficiently, such as when employees leave or temporary accounts are no longer needed.
| Error | Cause | Solution |
|---|---|---|
| Insufficient privileges to complete the operation | Missing admin permission | Reconnect using Connect-MgGraph -Scopes "User.ReadWrite.All" |
| User not found | Incorrect UPN or ID | Verify that the UserPrincipalName or ID exists |
| Access denied | Account lacks sufficient privileges | Run PowerShell as administrator or use a global admin account |
The Remove-MgUser cmdlet is an essential tool for Microsoft 365 administrators who want to maintain a clean and secure environment. Whether you’re deleting a single user, confirming actions interactively, or removing multiple accounts from a CSV file, these scripts give you the flexibility and safety you need.
Start by testing with -WhatIf mode to confirm your results — then confidently automate user cleanup in your Microsoft 365 tenant!
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