How to access OneDrive (Office 365) of a deleted user account (Powershell)

Today, one of my supervisors asked me if I could recover the data from a former employee's OneDrive (whose account has been deleted).

So I got right down to work and started researching on that topic.

The result is this script:

# Requirements:
# - Azure Powershell Module

# Connect with Sharepoint and change <organisation> to a working value
$UserCredential = Get-Credential
Connect-SPOService -Url https://<organisation>-admin.sharepoint.com -Credential $UserCredential

# Capture and Select a deleted OneDrive URL
$url = (Get-SPODeletedSite -IncludeOnlyPersonalSite | Out-GridView -PassThru).URL

# Restore One Drive
Restore-SPODeletedSite -Identity $url

# Transfer rights to the person that should gain access using the persons <upn>
Set-SPOUser -Site $url -LoginName <upn> -IsSiteCollectionAdmin $True

# Open the recovered OneDrive web page with the standard browser
Start-Process $url

Additional helpful information:

  • the default retention period for a deleted OneDrive is 30 days but you can extend this by using this cmdlet
    • SetSPOTenant -OrphanedPersonalSitesRetentionPeriod <int32>

Reference:

https://docs.microsoft.com/en-us/powershell/sharepoint/sharepoint-online/connect-sharepoint-online?view=sharepoint-ps

https://docs.microsoft.com/en-us/onedrive/restore-deleted-onedrive

https://docs.microsoft.com/en-us/onedrive/retention-and-deletion