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?
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 "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 administrators rights, you can use "Site collection Recycle bin" and restore items for any site in the site collection. Here is how:
  • 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 "Recycle Bin" link from the "Site Collection Administration" section. Now, you can follow the same steps to restore any item in 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 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."

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 = "salaudeen@crescent.com"

#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
     
    #Restore items from Recylce bin
    $DeletedItemsByUser | ForEach-Object { $_.Restore(); Write-host "Restored Itme:"$_.Title }
    $Ctx.ExecuteQuery()    
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
Similarly, we can restore items deleted based on deleted date:
#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 gets 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: Restore All Items in Recycle Bin
To restore all Items from the recycle bin, simply remove the "where" clause from the script!
Here is my another post on restoring files from recycle bin: SharePoint Online: Restore Files from Recycle bin using PowerShell
SharePoint Online: Restore Deleted Items from Recycle Bin using PowerShell SharePoint Online: Restore Deleted Items from Recycle Bin using PowerShell Reviewed by Salaudeen Rajack on February 21, 2019 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.