Managing user accounts in Microsoft 365 involves handling both active and deleted users. When a user is deleted, they are first moved to a "soft delete" state, allowing for potential recovery within a specified period. In this article, we will explore how to restore and permanently delete users using Graph PowerShell. We will cover the key concepts of soft deletion and permanent deletion, use cases for these operations, and possible errors and their solutions.
Soft deletion is a state where a user account is removed from the active directory but is retained in a recycle bin for a certain retention period (typically 30 days). During this period, administrators have the option to restore the user account if needed.
Permanent deletion, also known as hard deletion, removes the user account completely from the directory. Once a user is permanently deleted, it cannot be recovered.
To manage deleted users, you first need to list them. The Get-MgDirectoryDeletedItemAsUser
cmdlet retrieves all the deleted users.
# List all deleted users
Get-MgDirectoryDeletedItemAsUser
This command provides a list of deleted users, including their IDs, which are necessary for restoration or permanent deletion.
To restore a deleted user, use the Restore-MgDirectoryDeletedItem
cmdlet along with the user's DirectoryObjectId
.
# Restore a deleted user
Restore-MgDirectoryDeletedItem -DirectoryObjectId c71e4a5f-e379-4389-8f6e-af9057860fa1
Replace c71e4a5f-e379-4389-8f6e-af9057860fa1
with the actual DirectoryObjectId
of the user you wish to restore.
To permanently delete a user, use the Remove-MgDirectoryDeletedItem
cmdlet with the user's DirectoryObjectId
.
# Permanently delete a user
Remove-MgDirectoryDeletedItem -DirectoryObjectId $directoryObjectId
Replace $directoryObjectId
with the actual DirectoryObjectId
of the user you wish to permanently delete.
Cause: The specified DirectoryObjectId
does not exist.
Solution: Verify the DirectoryObjectId
- which is the User ID in our case - actually exists by executing Get-MgUser cmdlet.
Managing user deletions in Microsoft 365 involves understanding the concepts of soft deletion and permanent deletion. Using Graph PowerShell, administrators can efficiently restore or permanently delete user accounts as required. By following the steps and examples provided, you can handle these operations smoothly and address any errors that may arise. Understanding these processes ensures better management of user lifecycle and data security within your organization.
By leveraging these PowerShell cmdlets, you can maintain control over your directory and ensure that user accounts are managed according to your organization's policies and needs.
© m365corner.com. All Rights Reserved. Design by HTML Codex