Powershell: Deleting Users in Microsoft 365

Intro

In large Microsoft 365 environments, administrators often need to bulk delete users—whether due to offboarding, tenant cleanup, or automation. Microsoft Graph PowerShell SDK enables safe and controlled user deletion directly from the command line.

In this article, we'll walk through how to delete users individually, in bulk, and with filters, while preserving admin accounts and providing optional CSV logging.

Prerequisites

Ensure you have the following:

  • Microsoft Graph PowerShell SDK installed

  • Global Administrator or appropriate role

  • Permission scopes: User.ReadWrite.All, Directory.ReadWrite.All

To install and connect to Microsoft Graph:

Install-Module Microsoft.Graph -Scope CurrentUser
Connect-MgGraph -Scopes "User.ReadWrite.All", "Directory.ReadWrite.All"

Delete a Single User by Email or ID

By UserPrincipalName:

Remove-MgUser -UserId "user@example.com"

By Object ID:

Bulk Delete Users (Except Admin)

Here’s a safe script to delete all users except a defined admin account:

Targeted Deletion Examples

Delete users from a specific domain:

Delete unlicensed users only:

Optional: Hard Delete from the Recycle Bin

After 30 days, or to manually remove users from soft-delete:

Summary

Microsoft Graph PowerShell enables full control over M365 identity management, including safe user deletion. Whether you're removing one user or cleaning up thousands, PowerShell scripts offer automation, accuracy, and safety when used responsibly.


REFERENCES

Last updated

Was this helpful?