skip to Main Content

Manage Office 365 recycle bin with PowerShell

In this article, we will show you how to use PowerShell commands to manage the recovery of deleted users in Office 365. A deleted user account will be kept in the Azure Active Directory recycle bin for 30 days by default. This is described as soft delete, because it is not completely deleted. We will show you how to restore deleted users and permanently remove deleted users from Office 365 with PowerShell.

Office 365 recycle bin

When we delete a user account, it will move to the recycle bin for 30 days in Azure Active Directory. You can find it by navigating to Microsoft 365 admin center > Users > Deleted users.

There are two ways of deleting Office 365 users:

  • Soft delete user is when you delete a user, and it moves to the Azure Active Directory recycle bin
  • Hard delete user is when you delete a user permanently, also removed from the Azure Active Directory recycle bin

Note: Hard delete should only be used as a last alternative when no other option is available. Once you hard delete a user, there is no opportunity to restore this user or any data associated with that user.

Manage Office 365 recycle bin deleted users

Let’s look into different ways to manage Office 365 deleted users:

  • Restore deleted users up to 30 days
  • Recover deleted users account that block creating a new user account with an identical display name or UserPrincipalName (UPN)
  • Remove deleted users from the recycle bin

Connect to Azure AD PowerShell

To be able to run the PowerShell commands specified in the current article, you will need to Connect to Azure AD PowerShell.

Start Windows PowerShell as administrator and run the cmdlet Connect-MsolService.

Connect-MsolService

Find Office 365 deleted user accounts

To get the Office 365 deleted user accounts in Microsoft 365 admin center, follow the below steps:

  1. Sign in to Microsoft 365 admin center
  2. Click on Users > Deleted users
Manage Office 365 Recycle bin with PowerShell deleted users

You can also find Office 365 deleted users with PowerShell in the next step.

Get specific Office 365 deleted user account

After connecting to Azure AD, you can get information about a single Office 365 deleted user.

PowerShell command syntax:

Get-MsolUser –ReturnDeletedUsers -UserPrincipalName "userprincipalname"

Run the below PowerShell command example to show information about a deleted user.

Get-MsolUser –ReturnDeletedUsers -UserPrincipalName "Rene.Gibs@m365info.com"

The PowerShell output shows information about a specific user.

PS C:\> Get-MsolUser –ReturnDeletedUsers -UserPrincipalName "Rene.Gibs@m365info.com"

UserPrincipalName      DisplayName isLicensed
-----------------      ----------- ----------
Rene.Gibs@m365info.com René Gibs   False   

Get a list of all Office 365 deleted user accounts

Run the below PowerShell command to find all the Office 365 deleted users.

Get-MsolUser -All -ReturnDeletedUsers

In our example, the PowerShell output looks like this.

PS C:> Get-MsolUser -All -ReturnDeletedUsers

UserPrincipalName                                                    DisplayName       isLicensed
-----------------                                                    -----------       -------
Rene.Gibs@m365info.com                                               René Gibs         False  
Soren.Larsen@m365info.com                                            Søren Larsen      True   
Melissa.Kelly@m365info.com                                           Melissa Kelly     True   
ExRemoved-1fd281349183420f861e730891800dd0@ms365info.onmicrosoft.com SharedMailboxTest False  
RoomMailboxTest@m365info.com                                         RoomMailboxTest   False  
Julia.Wood@m365info.com                                              Julia Wood        False  

Restore deleted Office 365 user account

You can restore deleted users in Microsoft 365 admin center and with PowerShell.

  • You can restore deleted users, and recover their data, except for calendar items and aliases, for up to 30 days from the deleted users list.
  • Restoring a deleted user will restore all associated data, assign product licenses, and give access to all services they could access before they were deleted.

Note: In Microsoft 365 admin center, you can only restore a single deleted user at a time, and there is no option to bulk restore deleted users. With PowerShell, it’s possible to restore a single deleted user and bulk restore deleted users.

Restore deleted Office 365 user account

PowerShell command syntax:

Restore-MsolUser -UserPrincipalName "userprincipalname"

Restore a deleted user with the below PowerShell command.

Restore-MsolUser -UserPrincipalName "Rene.Gibs@m365info.com"

After running the above command, you will find the user in Microsoft 365 admin center under Users > Active users.

Manage Office 365 Recycle bin with PowerShell restore deleted user

Bulk restore deleted Office 365 user accounts

In Microsoft 365 admin center, you can only restore one user at a time. It is much faster to bulk restore deleted Office 365 users account in one PowerShell command.

Run the below PowerShell command to bulk restore deleted Office 365 users.

Get-MsolUser -All -ReturnDeletedUsers | Restore-MsolUser

Restore deleted Office 365 user account and use Auto Reconcile Proxy Conflicts

Let’s say you created a new user account (Julia.Wood@m365info.com) that has the exact same UserPrincipalName (UPN) as the one in the deleted users recycle bin.

When you want to restore this specific deleted user “Julia.Wood@m365info.com” in PowerShell, you will get an error (ID and Proxy conflict).

PS C:\> Restore-MsolUser -UserPrincipalName "Julia.Wood@m365info.com"
Restore-MsolUser : The specified user account cannot be restored because of the following 
error: Error Type ProxyAddress, Error Id Conflicts, Current Value 
smtp:Julia.Wood@ms365info.onmicrosoft.com, Suggested Value , ObjectType User, Conflicting 
Object Id 60373081-0c07-42ca-9046-7c242b8be83e.
At line:1 char:1
+ Restore-MsolUser -UserPrincipalName "Julia.Wood@m365info.com"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [Restore-MsolUser], MicrosoftOnlineExcept 
   ion
    + FullyQualifiedErrorId : Microsoft.Online.Administration.Automation.RestoreUserErrorExce 
   ption,Microsoft.Online.Administration.Automation.RestoreUser

Use the below PowerShell command syntax to restore a deleted user with a small change to the original UPN.

For example, you can add “RESTORE” in the -NewUserPrincipalName.

Restore-MsolUser -UserPrincipalName "userprincipalname" -AutoReconcileProxyConflicts -NewUserPrincipalName "userprincipalnameRESTORE"

Run the below PowerShell command example.

Restore-MsolUser -UserPrincipalName "Julia.Wood@m365info.com" -AutoReconcileProxyConflicts -NewUserPrincipalName "JuliaRESTORE@m365info.com"

The PowerShell output shows that the deleted user is restored under a different name.

PS C:\> Restore-MsolUser -UserPrincipalName "Julia.Wood@m365info.com" -AutoReconcileProxyConflicts -NewUserPrincipalName "JuliaRESTORE@m365info.com"

UserPrincipalName         DisplayName isLicensed
-----------------         ----------- ----------
JuliaRESTORE@m365info.com Julia Wood  False     

To verify that the deleted user is restored, navigate to Microsoft 365 admin center > Users > Active users.

In our example, JuliaRESTORE@m365info.com is successfully restored, and Julia.Wood@m365info.com is still present in the active users list.

Manage Office 365 Recycle bin with PowerShell restore deleted same username

You successfully restored the deleted user with PowerShell.

Hard delete user account from the recycle bin

You can permanently remove deleted Office 365 users from the Azure Active Directory recycle bin with PowerShell.

Note: It’s impossible to permanently delete Office 365 users from the Microsoft 365 admin center. The only option is to connect to Azure AD PowerShell and force delete the users from there.

Hard delete single user account from the recycle bin

PowerShell command syntax:

Remove-MsolUser -UserPrincipalName "userprincipalname" -RemoveFromRecycleBin -Force

Run the below PowerShell command to remove a single deleted user permanently.

Remove-MsolUser -UserPrincipalName "Melissa.Kelly@m365info.com" -RemoveFromRecycleBin -Force

After running the above command, we can verify that the deleted user is not in the deleted users list.

Manage Office 365 Recycle bin with PowerShell hard-delete user permanently

Bulk hard delete user accounts from the recycle bin

Run the below PowerShell command to remove all deleted users from recycle bin permanently.

Get-MsolUser -All -ReturnDeletedUsers | Remove-MsolUser -RemoveFromRecycleBin -Force

Check if you permanently removed the deleted users from the recycle bin in Microsoft 365 admin center. Go to Microsoft 365 admin center > Users > Deleted users. The list should be clear, as shown in the below picture.

Permanently hard-delete Office 365 users from recycle bin with PowerShell

Export information about deleted Office 365 user accounts

You can export information about all the Office 365 deleted users in the recycle bin to a single CSV file. This way, you can search for the different users and see if they are licensed.

Export information about soft deleted Office 365 user accounts to CSV file

PowerShell command syntax:

Get-MsolUser -All -ReturnDeletedUsers | Select DisplayName,FirstName,LastName,IsLicensed,UserPrincipalName,WhenCreated,SoftDeletionTimestamp | Export-CSV -NoTypeInformation -Encoding UTF8

Use the below PowerShell command to export all deleted users information to a CSV file.

Get-MsolUser -All -ReturnDeletedUsers | Select DisplayName,FirstName,LastName,IsLicensed,UserPrincipalName,WhenCreated,SoftDeletionTimestamp | Export-CSV "c:\temp\Deleted Office 365 User accounts.CSV" -NoTypeInformation -Encoding UTF8

Once you export the information, you can find the CSV file in the C:\temp folder.

Open the CSV file with an application like Microsoft Excel to see the results.

Export information about deleted Office 365 users account to CSV file

Did this help you to manage Office 365 users with PowerShell?

Read more: Reset MFA for Microsoft 365 user »

Conclusion

You learned how to manage Office 365 recycle bin with PowerShell. Microsoft will automatically remove deleted users after 30 days (hard delete). The only option to bulk restore soft deleted users is with PowerShell. But with PowerShell, you can force remove the deleted users permanently from the Azure AD recycle bin. Also, you can export information about all deleted Office 365 users to a CSV file with PowerShell.

Did you enjoy this article? You may also like Export Microsoft 365 users licenses. Don’t forget to follow us and share this article.

The o365info Team

The o365info Team

This article was written by our team of experienced IT architects, consultants, and engineers.

This Post Has 0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *