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.

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

     Example: 
     .\Restore-DeletedOneDrive -DeletedUsername JohnDoe -NewUserEmail Calvin.Hobbes@acme.com

#>

Param($DeletedUsername,$NewUserEmail)


Function Connect-ProofpointSharePoint
{
    Import-Module Microsoft.Online.SharePoint.PowerShell
    Connect-SPOService -Url https://acme-admin.sharepoint.com 
}

<#
    -*-*-*-*-*-*-* The Script Starts Here -*-*-*-*-*-*-*-*-*-*

    IMPORTANT:  Repalce the OneDrive URL for the variable $DeletedUserOneDriveURL

    Example OneDrive URL: https://acme-my.sharepoint.com/personal/username_acme_com
    In this example, "acme" is the company's Office 365 tag. The email address username@acme.com 
    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 = "https://acme-my.sharepoint.com/personal/" + $DeletedUsername + "_acme_com"

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

"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 https://acme-admin.sharepoint.com

# 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:

WordPress.com Logo

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

Google photo

You are commenting using your Google 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