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 the “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 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 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

Salaudeen Rajack

Information Technology Professional with Two decades of SharePoint Experience.

11 thoughts on “Restore Deleted Items from Recycle Bin in SharePoint Using PowerShell

  • February 17, 2021 at 9:02 AM

    Hello!
    Great, but I get this error:

    The following exception occurred while trying to enumerate the collection: “”.
    At line:1 char:1
    $Item = $RecycleBin | Where{$_.Title -eq $ItemName}
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    CategoryInfo : NotSpecified: (:) [], ExtendedTypeSystemException
    FullyQualifiedErrorId : ExceptionInGetEnumerator

    Can you help?

    Kind regards,

    Klaus

    Reply
  • November 4, 2020 at 5:14 PM

    Hi there, thanks for the scripts is really helpful. I was just wondering if you are able to restore files based on the original location. For example restoring all files that were deleted in certain folder.

    Reply
  • May 22, 2020 at 7:05 AM

    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?

    Reply
  • May 19, 2020 at 6:23 PM

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

    Reply
    • May 21, 2020 at 11:17 AM

      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!

      Reply
  • March 4, 2020 at 3:32 PM

    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?

    Reply
    • May 21, 2020 at 11:21 AM

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

      Reply
  • April 23, 2018 at 7:59 PM

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

    Reply
    • April 24, 2018 at 7:19 AM

      Simple! Use:
      $Site = Get-SPSite $SiteURL

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

      Reply
    • April 25, 2018 at 9:24 PM

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

      Reply

Leave a Reply