Search code examples
azureazure-active-directoryazure-cli

Is it possible to restore an Azure Active Directory user using the azure-cli?


I recently deleted a user from Azure Active Directory and am looking to undo this action using Azure CLI. Before reaching out here, I tried looking up documentation and relevant commands but haven't found a clear solution yet.


Solution

  • To list the deleted users, make use of below command:

    az rest --method GET --url "https://graph.microsoft.com/v1.0/directory/deletedItems/microsoft.graph.user"
    

    enter image description here

    Note that: Deleted users will be available up to 30 days to restore. After 30 days the user will be permanently deleted and you will not be able to restore.

    To restore the user from Azure CLI, make use of below command:

    az rest --method POST --url "https://graph.microsoft.com/v1.0/directory/deletedItems/ObjectID/restore"
    

    enter image description here

    The user is restored successfully:

    enter image description here

    Otherwise, you can also make use of Azure PowerShell:

    To list the deleted users, make use of below commands

    Connect-MSOLService
    
    Get-MsolUser -ReturnDeletedUsers
    Restore-MsolUser -UserPrincipalName <string> -AutoReconcileProxyConflicts -NewUserPrincipalName <string>
    

    enter image description here