SharePoint Online: Restore a Document from the Recycle Bin using PowerShell

Requirement: Recover a deleted document from the recycle bin in SharePoint online.

How to Restore from Recycle Bin in SharePoint Online?
When you delete a document from the SharePoint Online document library, it just moved to the SharePoint Recycle Bin, similar to what happens when you delete a document on your local computer. There are two stages of recycle bins in SharePoint Online. When an item is deleted, it goes to the first-stage Recycle Bin (or end-user Recycle Bin). When you delete an item from the first-stage Recycle Bin, then it is moved to the second-stage Recycle Bin which can be accessed by Site Collection Administrators. The total lifetime of deleted items is 93 days. When it reaches the retention period of 93 days, the item gets deleted permanently from the Recycle Bin (doesn't matter if it's in first-stage or in second-stage).

To restore items from recycle bin,
  • Login to your SharePoint Online site and click "Site Contents" from either site settings gear or from the left navigation menu.
  • Click on the "Recycle bin" link from the top-right area of the page. This is called "End-user Recycle Bin" that lists all items deleted either by you or by someone else. (As long as you have edit access on the deleted items you can view and restore them from here!)
  • This page lists all deleted documents in SharePoint Online site. Select the document(s) you would like to restore and then click "Restore Selection".
    sharepoint online restore from recycle bin powershell
  • Confirm restore by clicking the "OK" button.
  • The files you selected should be available in the original location from where it was deleted.

Restore Deleted Documents from Admin Recycle bin: 
As the end-user recycle bin is security trimmed, You can use the Site collection Admin recycle bin to view and restore documents deleted by end-users. Login to the site as site administrator, Go to Site Settings and click on the "Recycle Bin" link under "Site Collection Administration" section (URL shortcut: "_layouts/15/AdminRecycleBin.aspx") to restore deleted files.

Recover Documents from Second Stage Recycle Bin:
Documents deleted from the first stage Recycle Bin (or end-user recycle bin) doesn't get deleted permanently as long as its retention period is lesser than 93 days. Instead, they are moved to the second-stage that can be accessed by the site collection administrator. Follow these steps to recover from second-stage Recycle Bin:
  • Log in to the SharePoint Online Site as the site collection administrator.
  • Click the Settings gear icon and choose Site Settings.
  • In the Site Settings page, click the Recycle Bin link under the Site Collection Administration section.
  • Click on the "Second-stage recycle bin" link from the bottom of the page. (URL short-cut for second Stage Recycle bin: /_layouts/15/AdminRecycleBin.aspx?view=13)
  • Select the items you wish to restore and click "Restore.
Now, Let's see the SharePoint Online PowerShell to restore items from the recycle bin.

SharePoint Online: Restore from Recycle bin using PowerShell
In SharePoint Online we can restore deleted files using PowerShell. E.g. Let's use PowerShell to restore all excel files from recycle bin to its original location.
#Load SharePoint Online Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
   
##Variables for Processing
$SiteUrl = "https://crescent.sharepoint.com/sites/Sales/"
$UserName="[email protected]"

#Get the password to connect 
$Password = Read-host -assecurestring "Enter Password for $UserName"
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,$Password)
 
Try {    
    #Setup the context
    $Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
    $Context.Credentials = $Credentials
    
    #Get the Site recycle bin
    $Site = $Context.Site
    $RecycleBinItems = $Site.RecycleBin
    $Context.Load($Site)
    $Context.Load($RecycleBinItems)
    $Context.ExecuteQuery()

    #Get all Excel files from Recycle bin
    $ExcelFiles= $RecycleBinItems | Where {$_.Title -like "*.xlsx"}
    Write-Host "Total Number of Excel Files found Recycle Bin:" $ExcelFiles.Count
    
    #Restore from Recylce bin
    $ExcelFiles | ForEach-Object { $_.Restore() }
    $Context.ExecuteQuery()    
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
Please note that the above script restores from 1st stage recycle bin (or end-user recycle bin). Instead of Web object, you may have to use Site object if you want to restore from the Site collection Recycle bin (Or 2nd stage recycle bin).

SharePoint Online: Restore a File from Recycle bin using PnP PowerShell 
To restore items from recycle bin, as a first step lets get the ID of an item to restore:
#Config Variables
$SiteURL = "https://crescenttech.sharepoint.com/sites/marketing"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Get All Items from Recycle bin
Get-PnPRecycleBinItem | Select Title, ID, ItemType, Size, ItemState, DirName, DeletedByName, DeletedDateLocalFormatted | Format-table -AutoSize 
This gets you all items in the recycle bin with their IDs.
Use -RowLimit switch to add pagination to recycle bin items and avoid threshold exceed issue: "The attempted operation is prohibited because it exceeds the list view threshold enforced by the administrator". E.g.  Get-PnPRecycleBinItem -SecondStage -RowLimit 5000
powershell to get recycle bin items in sharepoint online

Now, you can restore with the ID parameter
#Restore Recycle Bin Item by ID
Restore-PnPRecycleBinItem -Identity fd97143c-68fc-48ef-b148-5b6228610899 -Force

If you want to Restore all items from recycle bin, use:
#Restore All Items from recycle bin
Get-PnPRecycleBinItem | Restore-PnpRecycleBinItem

Similarly, you can restore a file by its name or type. E.g. Let's restore all documents (.docx)
Get-PnPRecycleBinItem | Where {$_.LeafName -like "*.docx"} | Restore-PnpRecycleBinItem -Force
Here is my other post on restoring deleted items from recycle bin: SharePoint Online: PowerShell to Restore Deleted Items from Recycle bin

5 comments:

  1. A colleague managed to delete a whole departments files (60,000 in 7700 folders) from Sharepoint Online - manually restoring them with the GUI has proved to nearly impossible (many timeout errors) so I stumbled upon your page in searching for a PowerShell solution. I'm a novice at PowerShell but it must surely be possible to restore all items in the recycle bin or even better all items in the recycle bin deleted by a specific user? Are you able to help with this?

    ReplyDelete
    Replies
    1. Sure, Just removed the "Where" class in Line#26 and run the script in PowerShell ISE!

      Delete
  2. How can you restore files deleted by a particular user within a specific time?

    ReplyDelete
    Replies
    1. That is exactly the same question I'd like to ask. I do not get the script to work properly at all.

      Delete

Please Login and comment to get your questions answered!

Powered by Blogger.