Office 365: How to restore deleted OneDrive in PowerShell?

If an employee leaves the company, few managers wants the OneDrive content of the ex-employee. Few occasions it is business critical to take the documents from ex-employees’ OneDrive and re-share it again.

So how do you do that? I wrote the following script that does exactly that.

Before you run the script, make sure you have installed SharePoint Online module. Here is the help: Get started with the SharePoint Online Management Shell | Microsoft Docs

Note: Deleted OneDrive will be purged after 90 days. You can only restore the deleted OneDrive within 90 days of disabled date.

    Script to 
    1. Restore the deleted OneDrive
    2. Assign the Restored OneDrive to manager or another employee

    IMPORTANT:  Replace the OneDrive URL for the variable $DeletedUserOneDriveURL and 
    admin URL in Connect-SPOService's URL parameter.

    DeletedUsername : ex-employees' username or alias (Usually the name in the email address)
    NewUserEmail: Who needs to have access to the restored deleted OneDrive

     .\Restore-DeletedOneDrive -DeletedUsername JohnDoe -NewUserEmail



Function Connect-ToSharePoint
    Import-Module Microsoft.Online.SharePoint.PowerShell
    Connect-SPOService -Url 

    -*-*-*-*-*-*-* The Script Starts Here -*-*-*-*-*-*-*-*-*-*

    IMPORTANT:  Repalce the OneDrive URL for the variable $DeletedUserOneDriveURL

    Example OneDrive URL:
    In this example, "acme" is the company's Office 365 tag. The email address 
    is converted to  username_acme_come in the end of the URL. If you are in doubt, visit
    your OneDrive in Browser, Copy the URL from teh address bar and use it here.

$DeletedUserOneDriveURL = "" + $DeletedUsername + "_acme_com"

# This command will ask for user credentials
"Connecting to SharePoint Online..."

"Restoring the OneDrive: $DeletedUserOneDriveURL" 
Restore-SPODeletedSite -Identity $DeletedUserOneDriveURL

"Assigning $NewUseremail to the Deleted OneDrive"
Set-SPOUser -Site $DeletedUserOneDriveURL -LoginName $NewUserEmail -IsSiteCollectionAdmin $True

# -*-*-*- End of the Script -*-*-*-*-*-*

If you need to check if the “Deleted OneDrive” is already purged or not, check with these commands:

#Connect to SharePoint Online : Replace "acme" with your company's Office 365 tag

Connect-SPOService -Url

# List all deleted sites includes OneDrive. Note "Days Remaining" the last column shows how many days left for purging

Get-SPODeletedSite -IncludePersonalSite

# List only the specific user

Get-SPODeletedSite -IncludePersonalSite | ? {$_.url -like '*JohnDoe*'}

I am sure this script and SharePoint commands will be helpful to you. Enjoy.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s