Delete or Restore SharePoint Recycle Bin Items based on Deleted Date using PowerShell

Requirement is to Restore all the Items deleted before last week from Recycle bin, which were already deleted from End-User Recycle bin (1st Stage Recycle bin).

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue 

$WebApp=get-spwebapplication "http://sharePoint.crescent.com"

foreach ($SPSite in $WebApp.Sites)
{
	#Contains both First Stage & Second Stage Recycle bin Items
	$SPRecycleBinItemCollection  = $SPSite.RecycleBin;

	#write-host "Processing Site: " $SPSite.RootWeb.Title "`n" 
	#write-host "Total No. of Items in Recycle bin: " $SPRecycleBinItemCollection.Count
	for ($i=$SPRecycleBinItemCollection.Count-1; $i -GE 0;  $i--)
	{
		# check whether the Recycle bin Item is a SecondStageRecycleBin Item
		if($SPRecycleBinItemCollection[$i].ItemState -eq [Microsoft.SharePoint.SPRecycleBinItemState]::SecondStageRecycleBin)
		{
			$deletedTime = $SPRecycleBinItemCollection[$i].DeletedDate;

			$selectedTime=(Get-Date).AddDays(-7)

			if ($selectedTime -LT $deletedTime)
			{
				$guid = $SPRecycleBinItemCollection[$i].ID;
				write-host "Item Restored:" $SPRecycleBinItemCollection[$i].Title
				$SPRecycleBinItemCollection.Restore($guid);
				#$SPRecycleBinItemCollection.Delete($guid);
			}
		}
	}
}

Salaudeen Rajack

Information Technology Professional with Two decades of SharePoint Experience.

One thought on “Delete or Restore SharePoint Recycle Bin Items based on Deleted Date using PowerShell

  • September 6, 2014 at 9:38 PM

    Thank you, Thank you, Thank you! This script saved me hours of work.

    Reply

Leave a Reply