SharePoint Online: Restore Deleted Items from Recycle Bin using PowerShell

Requirement: Restore Items from recycle bin in SharePoint Online using PowerShell

How to Restore Items from Recycle bin in SharePoint Online?
The recycle bin in SharePoint Online site is more or less similar to what you have in your local PC. It lets you view and restore the items that have been deleted from your site. To restore Items from SharePoint Online recycle bin, do the following:
  • Navigate to the SharePoint Online site where you want to restore deleted items from the recycle bin. 
  • Click on Settings gear >> Choose the "Site Contents" link from the site settings menu.
  • On the Site Contents page, click the Recycle Bin link, located to the top-right section. You will be navigated to the Recycle Bin page for the site (not site collection!), which lists all items in the recycle bin.
    sharepoint online recycle bin restore powershell
  • To restore items from the site recycle bin, On the Recycle Bin page, check the box for each Item you want to restore and click the "Restore Selection" link.
    sharepoint online restore from recycle bin powershell
The selected items are restored, and the Recycle Bin page is refreshed to reflect the change.

Restore Deleted Items from Site Collection Recycle bin
If you have Site collection administrator rights, you can use "Site collection Recycle bin" and restore items for any site in the site collection. Here is how to restore a folder in SharePoint Online:
  • Navigate to the top-level site in the site collection (Root Web).
  • Click on Settings >> Select "Site Settings" link from the Settings menu.
  • On the Site Settings page, click on the "Recycle Bin" link from the "Site Collection Administration" section. Now, you can follow the same steps to restore any item in the Site collection Administrator recycle bin.
    sharepoint online recycle bin restore powershell
Make sure, You don't have a file or folder with the same name in the recycle bin. Otherwise, you'll end up with an error:  "A folder with this name "Folder Name" already exists in "Location". To restore the folder, rename the existing folder and try again.". Similarly, we can restore deleted items such as a file, folder using PowerShell as well.

How to check who has deleted a file in SharePoint Online? Just look at the "Deleted by" Column!

SharePoint Online: PowerShell to Restore Items from Recycle Bin
Let's restore all items deleted by a particular user in a SharePoint Online site collection:
#Load SharePoint Online Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
    
##Variables for Processing
$SiteUrl = "https://crescent.sharepoint.com/sites/marketing/"
$DeletedByUserEmail = "[email protected]"

#Get Credentials to connect
$Cred= Get-Credential
  
Try {    
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
     
    #Get the Site recycle bin
    $Site = $Ctx.Site
    $RecycleBinItems = $Site.RecycleBin
    $Ctx.Load($Site)
    $Ctx.Load($RecycleBinItems)
    $Ctx.ExecuteQuery()
 
    #Get all items deleted by a specific user
    $DeletedItemsByUser = $RecycleBinItems | Where {$_.DeletedByEmail -eq $DeletedByUserEmail}
    Write-Host "Total Number of Items Deleted by User in Recycle Bin:" $DeletedItemsByUser.Count
     
    #sharepoint online restore deleted files using PowerSehll
    $DeletedItemsByUser | ForEach-Object { $_.Restore(); Write-host "Restored Itme:"$_.Title }
    $Ctx.ExecuteQuery()    
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
This PowerShell recovers deleted files. Similarly, we can restore items deleted based on the deleted date.

SharePoint Online PowerShell to Restore Items from Recycle Bin
Let's restore all items deleted today!
#Load SharePoint Online Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
    
##Variables for Processing
$SiteUrl = "https://crescent.sharepoint.com/sites/marketing/"
$DeletedDate = (Get-Date).Date #Get Today's Date

#Get Credentials to connect
$Cred= Get-Credential
  
Try {    
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
     
    #Get the Site recycle bin
    $Site = $Ctx.Site
    $RecycleBinItems = $Site.RecycleBin
    $Ctx.Load($Site)
    $Ctx.Load($RecycleBinItems)
    $Ctx.ExecuteQuery()
 
    #Get all items deleted on given date
    $DeletedItems = $RecycleBinItems | Where { ($_.DeletedDate).Date -eq $DeletedDate }
    Write-Host "Total Number of Items Deleted on given Date:" $DeletedItems.Count
     
    #Restore items from Recylce bin
    $DeletedItems | ForEach-Object { $_.Restore(); Write-host "Restored Item:"$_.Title }
    $Ctx.ExecuteQuery()    
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
} 
How about First Stage and Second Stage Recycle Bin?
By default, these scripts get deleted items from both stages! In CSOM: To Get Recycle bin items based its stage, use: ItemState -eq "FirstStageRecycleBin" or "SecondStageRecycleBin". Similarly, in PnP PowerShell: use the parameters either -FirstStage or -SecondStage with cmdlet: Get-PnPRecycleBinItem to filter recycle bin items accordingly!

PnP PowerShell to Restore Recycle bin Items in SharePoint Online 
Let's restore items deleted in the past 7 days using PnP PowerShell.
#Parameter
$SiteURL= "https://crescent.sharepoint.com/sites/marketing/"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Get All Items Deleted in the Past 7 Days
$DeletedItems = Get-PnPRecycleBinItem | Where { $_.DeletedDate -gt (Get-Date).AddDays(-7) }

#Restore Recycle bin items matching given query
$DeletedItems | Restore-PnpRecycleBinItem -Force

Similarly, you can restore all items deleted from a specific site, subsite, library or folder using:
#Parameter
$SiteURL= "https://crescent.sharepoint.com/sites/marketing/"
$DirPath = "sites/marketing/Shared Documents"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Get All Items deleted from a specific path or library
$DeletedItems = Get-PnPRecycleBinItem | Where { $_.DirName -like "$DirPath*"} | Restore-PnpRecycleBinItem -Force
SharePoint Online: Bulk Restore All Items in Recycle Bin
To restore all Items from the recycle bin, simply remove the "where" clause from the script!
Get-PnPRecycleBinItem | Restore-PnpRecycleBinItem
To restore all files deleted by a particular user, use:
Get-PnPRecycleBinItem | Where { $_.DeletedByEmail -eq "[email protected]"} | Restore-PnpRecycleBinItem -Force

Here is my another post on restoring files from recycle bin: SharePoint Online: Restore Files from Recycle bin using PowerShell

1 comment:

  1. How do I check if the item restored already exists in its original location? E.g. I've had a file in a folder, delete it and then re-uploaded. Deleted and re-uploaded again. Now, I want to restore the most recent file making sure it doesn't exists in the target. Please help.

    ReplyDelete

Please Login and comment to get your questions answered!

Powered by Blogger.