Restoring users in Microsoft Entra enables you to recover from situations where the user account was deleted in error or even if a user returns to the organisation shortly after their account was deleted. When events like these happen, there is a chance (especially in large environments) that the previous username was taken by another user, which would prevent the restore operation from taking place.
This conflict can be avoided by restoring the user object with a new username, which can be done using Microsoft Graph PowerShell, but not from the Microsoft 365 or Entra admin centres. In this article, I will show you how to restore users from the Microsoft Entra recycle bin with a new user principal name using Microsoft Graph PowerShell.
Requirements
The commands in this article are found within the beta Microsoft Graph PowerShell SDK. Two modules from within this SDK are required, these are:
- Microsoft.Graph.Authentication
- Microsoft.Graph.Beta.Identity.DirectoryManagement
If you are not familiar with how to install these modules, check out this article: How to install the Microsoft Graph PowerShell Modules.
To consent to the requirement permissions you will also need access to a Global Administrator account in your tenant.
To learn more about permissions and authentication, check out these permissions-related articles, alternative I go into more advanced detail on permissions and authentication in chapter 4 of my book Microsoft Graph PowerShell for Administrators. Use code OURCLOUDNETWORK15 for %15 off!
Restore users with a new username
Start by opening PowerShell and using the below command to connect to Microsoft Graph. You will be prompted to sign in with your Global Administrator account and grant consent to the User.DeleteRestore.All permission.
Connect-MgGraph -scopes User.DeleteRestore.All Next, you will store the deleted user object in a variable so you can call the ID property when performing the restore request. To do this, you will filter the deleted users list by username property which ends in the original username of the deleted user. This is because 32 random characters are prepended to the username once a user is deleted, as you can see from the below image.
Copy and paste the following code, while modifying the value of the $username variable to match the user you wish to restore.
#Enter the original username
$Username = "[email protected]"
#Advanced query header
$headers = @{"ConsistencyLevel"="eventual"}
#Filtering for the user
$Obj = Get-MgBetaDirectoryDeletedItemAsUser `
-Filter "endsWith(UserPrincipalName, '$Username')" `
-Headers $headers -CountVariable CountVar The body of the restore request needs to be defined into a hash table, like the one in the below example. Modify the $newUPN variable to match the desired new username of the restored user.
#Define the new user principal name
$newUPN = "[email protected]"
#Prepare the body of the request
$params = @{
newUserPrincipalName = $newUPN
}
#Restore the deleted user
Restore-MgBetaDirectoryDeletedItem `
-DirectoryObjectId $Obj.Id -BodyParameter $params You will now see from the Microsoft Entra admin center that the user has been successfully restored.

