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

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

How to Restore from Recycle Bin in SharePoint Online?
To restore items from recycle bin,
  • Login to your SharePoint online site and click "Site Contents" from either site settings gear or from left navigation.
  • Click on "Recycle bin" link from the top of the page.
  • Select the document(s) you would like to restore and then click "Restore Selection".
    sharepoint online restore from recycle bin
  • Confirm restore by clicking "OK" button.
  • The files you selected is available in the original location from where it was deleted. Now, Lets see the SharePoint Online PowerShell to restore items from 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. 
#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 = ""
$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 web recycle bin
    $Web = $Context.web
    $RecycleBinItems = $Web.RecycleBin

    #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() }
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 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 = ""

#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 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
SharePoint Online: Restore a Document from the Recycle Bin using PowerShell SharePoint Online: Restore a Document from the Recycle Bin using PowerShell Reviewed by Salaudeen Rajack on July 02, 2016 Rating: 5


  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?

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

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

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


Please Login and comment to get your questions answered!

Powered by Blogger.