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,
- Clear - Host
- 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"#
- Get the Site Collection URL
- $SiteUrl = Read - host "Enter the Site URL"
- #Get the Site Owners Credentials to connect the SharePoint
- $UserName = Read - host "Enter the Email ID"
- $Password = Read - host - assecurestring "Enter Password for $AdminUserName"
- $Credentials = New - Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $Password)
- # Once Connected, get the Site information using current Context objects
- Try {
- $Context = New - Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
- $Context.Credentials = $Credentials
- $Site = $Context.Site
- $RecycleBinItems = $Site.RecycleBin
- $Context.Load($Site)
- $Context.Load($RecycleBinItems)
- $Context.ExecuteQuery()
- # The above code will help to retrieve all items from the recycle bin on below table format
- $RecycleBinItems | Select Title, DeletedByEmail, DeletedDate, ItemType, ItemState | Format - Table - AutoSize
- # You can export the List in CSV format using Out - file command
- } catch {
- write - host "Error: $($_.Exception.Message)" - foregroundcolor Red
- }#Once exported, you can use below code to
- import the CSV file and get the item restored one by one
- $Location = Read - Host "Enter the CSV file path with filename and extension"
- $file = Import - Csv $Location
- $importitemcount = $file.Count
- # using for loop to restore the item one by one
- For($i = 0; $i - le $importitemcount; $i++) {
- $itemsre = $file[$i].Title
- $Restoreitem = $RecycleBinItems | Where - Object {
- $_.Title - eq $itemsre
- }
- $Restoreitem | % {
- $_.Restore()
- }
- $Context.ExecuteQuery()
- Write - Host $itemsre "Restored" - ForegroundColor Yellow
- }
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:
- $DeletedByUser = Get-Host “Enter the Deleted By user ID”
- $DeletedByUser = $RecycleBinItems | Where {$_.DeletedByEmail -eq $DeletedByUserAccount} | Format-Table
You can get the item filtered by using the relevant recycle bin object.