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 "https://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

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!

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

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

    Reply

Leave a Reply

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