SharePoint restore all items from Recycle bin with PowerShell

Visit our website to find out more useful solutions and services from Chiron Consulting

Hello Guys,

I recently had a situation where the user accidentally deleted big amount of files from SharePoint Online (about 10k). Restore them all manually would be a nightmare. So, I came up with a PowerShell script.
Looking around the Internet for a solution I found a few related articles but provided solutions either didn't work or worked just for the on-prem environment.

So, I developed my own PowerShell script. Probably someone can found it useful as well.



All right, the solution is pretty simple, just copy the script provided below and paste to the new PowerShell file.

I configured script to be executed from a 2nd Admin Recycle bin.
If you would like to restore items from a 1st level Recycle bin change line #19 to the Web instead of Site:

$Site = $ctx.Web

Here is the script itself:

#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"
$SiteUrl = ""
#Set the password to connect
$Password = ConvertTo-SecureString "P@ssword2018!" -AsPlainText -Force
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,$Password)
Try {   
    #Setup the context
    $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
    $ctx.Credentials = $Credentials
    #Get the web recycle bin
    $Site = $ctx.Site
    $RecycleBinItems = $Site.RecycleBin
    #Get all Excel files from Recycle bin
    $AllItems= $RecycleBinItems
    Write-Host "Total Number of Files found in Recycle Bin:" $AllItems.Count
    #Restore from Recylce bin
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red

Good luck with fixing all issues you have 🙂

Share the blog post on:

Leave a Reply

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