Home Blog SharePoint restore all items from Recycle bin with PowerShell

SharePoint restore all items from Recycle bin with PowerShell

Alex Pollard 31 Jul 2023

Visit our website to find out more useful solutions and services from Chiron IT https://chironit.com

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 that I could inside SharePoint restore all items from Recycle bin with PowerShell
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 = "https://bainbridgere.sharepoint.com/sites/SharePoint"
#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 applying the script and restoring items.

Leave a Reply

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

Thank you for your comment

Your comment has been successfully submitted and will be published immediately after moderation.

Have a great day!

Have questions?

    Contact me

    Thank you for reaching out!

    Your message has been successfully delivered. I will be in touch with you shortly to discuss your inquiry further.

    If you have any urgent matters or need immediate assistance, please don't hesitate to contact me directly at my email or phone. I look forward to connecting with you and assisting you with your needs.

    Have a great day!
    Contact Information
    This website uses cookies

    This website uses cookies. By continuing, I assume your permission to deploy cookies as detailed in the Privacy Policy.