Restore SharePoint Item From Recycle Bin Using PowerShell

This blog may help you to restore multiple items from the SharePoint Recycle Bin by using PowerShell. Traditionally SharePoint has had the option to restore multiple files from the recycle bin but this will not help in all cases, because Microsoft recommends that only 200 items can be restored at a time.

Just consider, one user has deleted more than 20K documents accidently from SharePoint and they are trying to restore then from the recycle bin, but as per the Microsoft recommendation they can restore only 200 items at a time, so just calculate how long it will take to restore all 20K documents. I will take more than an hour to restore all deleted items.

So here we are going to restore the deleted items from the recycle bin using CSOM (Client -Side Object Model) with the help of PowerShell.

Let’s see the PowerShell Code, before that we need to download the SharePoint Online Client Components SDK from Microsoft.

First add the SharePoint Client dll’s for reference,
  1. Clear - Host  
  2. Add - Type - Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"  
  3. Add - Type - Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"#  
  4. Get the Site Collection URL  
  5. $SiteUrl = Read - host "Enter the Site URL"  
  6. #Get the Site Owners Credentials to connect the SharePoint  
  7. $UserName = Read - host "Enter the Email ID"  
  8. $Password = Read - host - assecurestring "Enter Password for $AdminUserName"  
  9. $Credentials = New - Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $Password)  
  10. # Once Connected, get the Site information using current Context objects  
  11. Try {  
  12.     $Context = New - Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)  
  13.     $Context.Credentials = $Credentials  
  14.     $Site = $Context.Site  
  15.     $RecycleBinItems = $Site.RecycleBin  
  16.     $Context.Load($Site)  
  17.     $Context.Load($RecycleBinItems)  
  18.     $Context.ExecuteQuery()  
  19.     # The above code will help to retrieve all items from the recycle bin on below table format  
  20.     $RecycleBinItems | Select Title, DeletedByEmail, DeletedDate, ItemType, ItemState | Format - Table - AutoSize  
  21.     # You can export the List in CSV format using Out - file command  
  22. catch {  
  23.     write - host "Error: $($_.Exception.Message)" - foregroundcolor Red  
  24. }#Once exported, you can use below code to  
  25. import the CSV file and get the item restored one by one  
  26. $Location = Read - Host "Enter the CSV file path with filename and extension"  
  27. $file = Import - Csv $Location  
  28. $importitemcount = $file.Count  
  29. # using for loop to restore the item one by one  
  30. For($i = 0; $i - le $importitemcount; $i++) {  
  31.     $itemsre = $file[$i].Title  
  32.     $Restoreitem = $RecycleBinItems | Where - Object {  
  33.         $_.Title - eq $itemsre  
  34.     }  
  35.     $Restoreitem | % {  
  36.         $_.Restore()  
  37.     }  
  38.     $Context.ExecuteQuery()  
  39.     Write - Host $itemsre "Restored" - ForegroundColor Yellow  
  40. }  
By the same method, you can filter the recycle bin items by ItemName, DeletedBy, ItemState, ItemType like below, Just replace line number 26 from the above script:
  1. $DeletedByUser = Get-Host “Enter the Deleted By user ID”  
  2. $DeletedByUser = $RecycleBinItems | Where {$_.DeletedByEmail -eq $DeletedByUserAccount} | Format-Table  
You can get the item filtered by using the relevant recycle bin object.