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("http://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

Information Technology Professional with Two decades of SharePoint Experience.

2 thoughts on “Search SharePoint Recycle bin with SPRecycleBinQuery

  • June 17, 2016 at 11:52 AM

    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
  • May 29, 2014 at 12:12 AM

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

    Reply

Leave a Reply