Restoring a Deleted OneDrive Site using PnP PowerShell

Loading

When a OneDrive site is deleted, it remains in the Deleted Sites list for a retention period (typically 93 days). During this time, administrators can restore the site using PnP PowerShell before it is permanently removed.

Using PnP PowerShell, you can:
Check if a OneDrive site is deleted
Restore a deleted OneDrive site
Verify restoration success


Prerequisites

1️⃣ Install PnP PowerShell (if not installed)

Ensure you have the latest version of PnP PowerShell installed:

Install-Module -Name PnP.PowerShell -Force -AllowClobber

2️⃣ Connect to SharePoint Admin Center

To manage deleted sites, connect to SharePoint Online:

Connect-PnPOnline -Url "https://yourtenant-admin.sharepoint.com" -Interactive

Replace "yourtenant" with your actual tenant name.

Use Global Administrator or SharePoint Administrator credentials.


Step 1: Retrieve Deleted OneDrive Sites

To list all deleted sites (including OneDrive sites):

Get-PnPTenantRecycleBinItem | Select-Object Url, DeletionTime, State

Displays all deleted sites with URLs and deletion timestamps.
State should be Recycled (not yet permanently deleted).

Filter Only OneDrive Sites
Since OneDrive URLs follow the pattern:
https://yourtenant-my.sharepoint.com/personal/username_domain_tld

You can filter only OneDrive sites:

Get-PnPTenantRecycleBinItem | Where-Object { $_.Url -like "*-my.sharepoint.com/personal/*" }

Lists only deleted OneDrive sites.


Step 2: Restore a Deleted OneDrive Site

Once you have identified the deleted OneDrive site URL, restore it using:

Restore-PnPTenantRecycleBinItem -Url "https://yourtenant-my.sharepoint.com/personal/username_domain_tld"

Restores the OneDrive site to its original state.

Restore All Deleted OneDrive Sites

Get-PnPTenantRecycleBinItem | Where-Object { $_.Url -like "*-my.sharepoint.com/personal/*" } | ForEach-Object { Restore-PnPTenantRecycleBinItem -Url $_.Url }

Restores all deleted OneDrive sites in bulk.


Step 3: Verify the Restoration

To confirm the OneDrive site is restored, check the active sites list:

Get-PnPTenantSite | Where-Object { $_.Url -like "*-my.sharepoint.com/personal/*" }

If the site appears, restoration was successful!

Alternative Verification Methods:
1️⃣ Try accessing the site in a browser:
https://yourtenant-my.sharepoint.com/personal/username_domain_tld
2️⃣ Check Microsoft 365 Admin Center under Active Sites.


Step 4: Automate OneDrive Site Restorations

To automate site restoration for all deleted OneDrive sites, create a script:

OneDrive Restore Script (Restore-OneDriveSites.ps1)

# Connect to SharePoint Admin Center
Connect-PnPOnline -Url "https://yourtenant-admin.sharepoint.com" -Interactive

# Retrieve and restore all deleted OneDrive sites
$deletedSites = Get-PnPTenantRecycleBinItem | Where-Object { $_.Url -like "*-my.sharepoint.com/personal/*" }

foreach ($site in $deletedSites) {
Restore-PnPTenantRecycleBinItem -Url $site.Url
Write-Host "Restored: $($site.Url)"
}

Write-Host "All deleted OneDrive sites have been restored successfully!"

Saves time by automating bulk restorations.

Schedule the Script using Windows Task Scheduler for periodic checks.


Step 5: Troubleshooting Issues

1️⃣ Error: “Access Denied”

Ensure you have SharePoint Admin or Global Admin role.
Run PowerShell as Administrator.

2️⃣ Error: “Site Not Found in Recycle Bin”

The site may be permanently deleted (after 93 days).
Check using:

Get-PnPTenantSite | Where-Object { $_.Url -like "*-my.sharepoint.com/personal/*" }

If missing, restore from a backup if available.

Leave a Reply

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