Simplify user tasks like bulk creation, updates, password resets, deletions, license checks & more — all from one place.
🚀 Launch ToolkitThe Restore-MgDirectoryDeletedItem cmdlet lets you recover deleted Azure AD (Entra ID) objects such as users and Microsoft 365 groups. When an object is deleted, it’s retained in a soft-deleted state for a limited period, allowing easy restoration before permanent removal. This cmdlet provides a safe and direct way to undo accidental deletions from your tenant.
Restore-MgDirectoryDeletedItem -DirectoryObjectId <directory-object-id>
Required scope (recommended):
Connect-MgGraph -Scopes "Directory.ReadWrite.All"
Use the correct helper depending on what you’re restoring.
# List deleted users and pick one
Get-MgDirectoryDeletedItemAsUser | Select-Object Id, DisplayName, UserPrincipalName, DeletedDateTime | Sort-Object DeletedDateTime -Descending
# List deleted groups and pick one
Get-MgDirectoryDeletedItemAsGroup | Select-Object Id, DisplayName, Mail, GroupTypes, DeletedDateTime | Sort-Object DeletedDateTime -Descending
Note down the Id of the object you want to restore.
Restore-MgDirectoryDeletedItem -DirectoryObjectId ae22d08d-1ef5-4ac0-b36b-376864f6b63a
# For a user (look for it in active users now)
Get-MgUser -Filter "userPrincipalName eq 'amanda@contoso.com'" -All
# For a group (look for it in active groups)
Get-MgGroup -Filter "displayName eq 'Finance - AP'" -All
You can restore all soft deleted Microsoft 365 Directory Objects by piping the results from Get-MgDirectoryDeletedItemAsUser or Get-MgDirectoryDeletedItemAsGroup to Restore-MgDirectoryDeletedItem cmdlet and looping through each item in the list using a ForEach-Object cmdlet.
Get-MgDirectoryDeletedItemAsUser -All | ForEach-Object { Restore-MgDirectoryDeletedItem -DirectoryObjectId $_.Id -Confirm:$false }
Note: You can also confirm the deletion by running Get-MgDirectoryDeletedItemAsUser or Get-MgDirectoryDeletedItemAsGroup, depending on your requirements.
| Error | Cause | How to Fix |
|---|---|---|
| Authorization_RequestDenied or Insufficient privileges to complete the operation. | Missing scopes or insufficient admin role. | Reconnect with Connect-MgGraph -Scopes "Directory.ReadWrite.All" and ensure your role (e.g., User/Group/Directory admin) is adequate. |
| ResourceNotFound / Request_ResourceNotFound | The ID doesn’t exist in the deleted container (typo, already restored, or permanently deleted). | Re-list deleted items with Get-MgDirectoryDeletedItemAsUser or ...AsGroup and pick a current Id. |
| Request_BadRequest with message about property/identifier conflict | The original UPN/MailNickname/ProxyAddress is in use by another object created after deletion. | Rename or release the conflicting identifier on the current object, then retry the restore. |
| Restore succeeds but user can’t sign in | Password/Conditional Access/licensing not set as before; sync lag for hybrid tenants. | Re-assign license(s), verify sign-in status and Conditional Access, and allow directory sync (if hybrid). |
| Group restored but not visible immediately in clients | Service caches and replication delays. | Wait a few minutes, then refresh clients/portal; verify via Get-MgGroup first. |
| Forbidden | You’re connected with delegated permissions from an account without rights, or using application identity without required app role. | Use a privileged admin account or an app registration granted Directory.ReadWrite.All (application) consent by an admin. |
Restore-MgDirectoryDeletedItem provides a reliable way to recover users or groups deleted by mistake in Azure AD. By first identifying the deleted object with Get-MgDirectoryDeletedItemAsUser or ...AsGroup, and then restoring it with a single command, administrators can quickly reverse accidental deletions. Ensure proper permissions, resolve naming conflicts, and verify post-restore configurations for a smooth recovery process.
© m365corner.com. All Rights Reserved. Design by HTML Codex