Restore Deleted Items from Recycle Bin in SharePoint Using PowerShell

How to restore deleted items from SharePoint recycle bin?
  • To restore deleted items, Click on "Recycle Bin" link on the Quick Launch.
  • Locate the object that you want to restore by Selecting the check box next to the object and then click Restore Selection.
    sharepoint restore from recycle bin powershell

SharePoint: Restore from recycle bin using PowerShell
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Configuration variables
$SiteURL="http://portal.crescent.com/sites/ops/"
$ItemName="Classifieds.xlsx" #Can be a List Name, File Name or ID of an Item

#Get Objects
$site = Get-SPSite $SiteURL
$RecycleBin = $site.RecycleBin

#Get the Item from Recycle bin
$Item = $RecycleBin | Where{$_.Title -eq $ItemName}

if($Item -ne $null)
{
 $Item.Restore()
 Write-Host "Item Restored from Recycle Bin!" -f DarkGreen
}
else
{
 Write-Host "No Item Found with the given name!" -ForegroundColor RED
}
This script restores given items from recycle bin. What if a particular file is deleted more than once? What if the original location of the file has another file with the same name?
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
 
#Configuration variables
$SiteURL="http://intranet.crescent.com"
$ItemName="Juneau.docx" 

#Get necessary objects
$Site = Get-SPSite $SiteURL
$Web = Get-SPWeb $SiteURL
$RecycleBin = $Site.RecycleBin
 
#Get the latest Item deleted from Recycle bin
$DeletedItem = $RecycleBin | Where {$_.Title -eq $ItemName} | Sort-Object DeletedDate -Descending | Select -First 1

If($DeletedItem -ne $Null)
{
    #Get the Original location of the deleted file
    $OriginalLocation = $DeletedItem.DirName+"/"+$DeletedItem.LeafName

    #Check if file exists
    If(!$Web.GetFile($OriginalLocation).Exists)
    { 
        $DeletedItem.Restore()
        Write-Host "Deleted File restored Successfully!" -f Green
    }
    else
    {
        Write-Host "There is an another item with the same name!" -f Yellow
    }
}
Else
{
    Write-Host "No Item Found with the given name!" -f Yellow
}
This script gets the latest file from recycle bin based on deleted date, Checks if the original location has a file with the same name. If not, it restores the file. Otherwise it skips.

Tips: How to find and restore all items deleted by a particular user? Use: $RecycleBin | Where { $_.DeletedBy -like "Domain\Account" }

Restore based on Object Type (such as List, Web, List Item, etc.)
What if you want to restore all deleted lists or deleted sub-sites from the Recycle bin? Here is the PowerShell script to restore all deleted lists from SharePoint 2013 recycle bin.
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Configuration variables
$SiteURL="http://portal.crescent.com"

#Get Objects
$site = Get-SPSite $SiteURL
$RecycleBin = $site.RecycleBin

#Get All deleted Lists from Recycle bin
$DeletedItems = $RecycleBin | Where{ $_.ItemType -eq "List"}
if($DeletedItems)
{
 Foreach($Item in $DeletedItems)
 {
  $Item.Restore()
  Write-Host "'$($Item.Title)' Restored from Recycle Bin!" -f DarkGreen
 }
}

PowerShell to restore all items from SharePoint Recycle bin:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

$SiteURL="http://portal.crescent.com/sites/operations"
$Site = Get-SPSite $SiteURL

#Get All Recycle bin items
$DeletedItems = $Site.RecycleBin

if($DeletedItems)
{
 foreach($Item in $DeletedItems) 
 { 
  $Site.RecycleBin.restore($Item.ID)
  Write-Host "Item restored:"$Item.Title
 }
}

Restore All Files from SharePoint Recycle bin using PowerShell:
Let's restore all files from SharePoint recycle bin, skip if a file with same name exists in the original location.
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
 
#Parameter
$SiteURL="http://sharepoint.crescent.com"

#Get necessary objects
$Site = Get-SPSite $SiteURL
$Web = Get-SPWeb $SiteURL
$RecycleBin = $Site.RecycleBin
 
#Get deleted Items from Recycle bin - sorted by deleted date in Descending order
$DeletedItems = $RecycleBin | Where {$_.ItemType -eq "File"} | Sort-Object DeletedDate -Descending

$DeletedItems | ForEach-Object {
    #Get the Original location of the deleted file
    $OriginalLocation = $_.DirName+"/"+$_.LeafName

    #Check if file exists
    If(!$Web.GetFile($OriginalLocation).Exists)
    { 
        $_.Restore()
        Write-Host "$($_.LeafName) restored Successfully!" -f Green
    }
    Else
    {
        Write-Host "There is an another file with the same name.. Skipping $($_.LeafName)" -f Yellow
    }
}
To restore deleted items from Recycle bin in SharePoint Online, Refer: SharePoint Online: Restore Deleted Items from Recycle Bin using PowerShell

9 comments:

  1. Hello Guys! How could I restore all items deleted via a particular date?

    ReplyDelete
    Replies
    1. Simple! Use:
      $Site = Get-SPSite $SiteURL

      #Get All deleted Lists after 01/01/2018
      $DeletedItems = $Site.RecycleBin | Where { $_.DeletedDate -gt "01/01/2018"}

      Delete
    2. For Sharepoint Online 16 isn't working, I have to change spsite to SPOSITE , but the command isn't reporting nothing, anything happening.

      Delete
  2. thanks for the script, can we only restore certain type of documents, lets say pdfs, with items deleted on 2/3/2020?

    please advise as a ton of documents get deleted and they are in the recycle bin?

    ReplyDelete
    Replies
    1. Sure, Use:
      #Get all PDF files deleted on given date
      $DeletedItems = $RecycleBinItems | Where { ($_.DeletedDate).Date -eq $DeletedDate -and $_.Title -like "*.pdf"}

      Delete
  3. Can you restore into specific location in Sharepoint Online rather than the parent folder where the items originate from?

    ReplyDelete
    Replies
    1. Unfortunately, No! The Deleted Items can be restored only to its original location! There are no ways to directly restore a deleted item to different location. You have to restore and then move!

      Delete
  4. Thanks for the script and explanation! When i run it i get errors because of duplicate items - is there a way to skip them and only restore files that are not in the folders already?

    ReplyDelete
    Replies
    1. Sure! post updated to skip if the file already exists.

      Delete

Please Login and comment to get your questions answered!

Powered by Blogger.