Search SharePoint Recycle bin with SPRecycleBinQuery

Searching inside the SharePoint recycle bin for a deleted item or file is a pain especially when you have a LOT of deleted items and files in the SharePoint recycle bin. Unfortunately, SharePoint doesn’t provide search capability in recycle bin, and you have to press Next>> <<Prev buttons to search for your file.

We can perform Search on SharePoint Recycle bin items with SPRecycleBinQuery. Here is an example of SPRecycleBinQuery with PowerShell.

Search SharePoint recycle bin using PowerShell

Good news is: If you know your file name, can search inside recycle in programmatically with PowerShell!

[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

#Get the Site collection
$site = new-object Microsoft.SharePoint.SPSite("https://sharepoint.company.com")

#Create new object for SPRecycleBinQuery
$query = new-object Microsoft.SharePoint.SPRecycleBinQuery

#$query.ItemState = "FirstStageRecycleBin"
$query.ItemState = "SecondStageRecycleBin"

#How many Rows to be returned
$query.RowLimit = 100

#Call GetRecycleBinItems to Search Inside SharePoint Recycle Bin
$DeletedItemsColl = $site.GetRecycleBinItems($query)

#Filter Result
$Result= $DeletedItemsColl | where  {$_.Title -match ".txt"}

#Write output
$Result | foreach-object {
	(Write-Host "Found Item:" $_.Title)
	(write-host "Created by:" $_.AuthorName)
	(write-host "Deleted by:" $_.DeletedByName)
	(write-host  "Deleted Date:"$_.DeletedDate)
	(write-host  "File Size(Bytes):" $_.Size)
	(write-host  "Original Location:" $_.DirName)
	(write-host "Web:" $_.web.URL)
	(Write-Host "------------------------------------")
}

We can search recycle bin items based on File Name, Deleted By, Deleted Time, Size, etc. Its also possible to either restore or permanently delete the files by calling approximate method.This is extremely useful if you want to search recycle bin programmatically!

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

2 thoughts on “Search SharePoint Recycle bin with SPRecycleBinQuery

  • Thanks for this, this saved my life 3 years later… the C# code for this
    SPSite site = new SPSite(“https://portal.denallix.com”);
    site.RecycleBin.DeleteAll();
    site.Dispose();
    for anyone who was looking for this

    Reply
  • Don’t forget to add this at the bottom: $site.Dispose()

    Reply

Leave a Reply

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