Restore Deleted Items from Recycle Bin in SharePoint Using PowerShell

How to restore deleted items from SharePoint recycle bin?

PowerShell is a powerful scripting language that provides administrators with a lot of control over their SharePoint environment. If you’re like most SharePoint administrators, you occasionally need to restore deleted items from your site. In SharePoint, when an item is deleted, it is moved to the Recycle Bin. From there, you can restore it if you change your mind or if it was deleted by mistake. This blog post will show you how to use PowerShell to restore deleted items in SharePoint.

To restore deleted items, follow these steps:

  • Navigate to your SharePoint site >> 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

Here is how to use PowerShell to restore items from the Recycle Bin:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Configuration variables
$SiteURL="https://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 file’s original location has another file with the same name?

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
 
#Configuration variables
$SiteURL="https://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 the recycle bin based on the deleted date and 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="https://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="https://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 the same name exists in the original location.

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
 
#Parameter
$SiteURL="https://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

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!

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

  • Can you get all deleted items by a specific user instead of using DeletionDate criteria

    Reply
  • 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
  • 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
  • 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
  • Can you restore into specific location in SharePoint Online rather than the parent folder where the items originate from?

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

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

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

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

      Reply
    • 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

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