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);
}
}
}
}
Thank you, Thank you, Thank you! This script saved me hours of work.