Restore Recycle Bin Items Of SharePoint Online Using PowerShell

In this blog, I will demonstrate how to restore items from the recycle bin of the SharePoint Online Site using SharePoint PnP PowerShell Script.
In my scenario a specific user has deleted 2000 files by mistake. The user was not ready to restore the files from the Recycle Bin. Instead they asked the SharePoint Online Support team to do the Job. Restoring individual files deleted by a specific user might take some effort so we have come up  with a PowerShell script solution.
Pre-requisites( Environment Details)
  • Windows PowerShell
  • SharePointPnpPowerShellOnline Module
Please install the SharePointPnpPowerShellOnline module if it’s not already present using the below command.
  1. Install-Module SharePointPnPPowerShellOnline  
SharePoint Recycle Bin
Variables Explanations in this blog
  • $O365ServiceAccount=""# Your Service Account Name
  • $O365ServiceAccountPwd="Test123$"#Your Service Account Password
  • $SharePointSiteURL="" # Change this SharePoint Site URL
  • $UserEmailWhoDeletedFiles="" # Change the User Email who deleted Files
Here you can see we have provided the password in Plain text which you need to provide if you would want this PowerShell script to run automatically through a Timer Job or Scheduler.
For Manual Execution please use Get-Credential command to read the User Name and Password from the user input.
Connect to SharePoint Online
Before Restoring the Files to SharePoint Online you should be connecting to the SharePoint Site using the below Snippet.
  1. [SecureString]$SecurePass = ConvertTo-SecureString $O365ServiceAccountPwd -AsPlainText -Force  
  3. [System.Management.Automation.PSCredential]$PSCredentials = New-Object System.Management.Automation.PSCredential($O365ServiceAccount, $SecurePass)  
  5. #Connecting to SharePoint Online site  
  7. Connect-PnPOnline -Url $SharePointSiteURL -Credentials $PSCredentials  
Get Recycle Bin Items Deleted by User
We have to get all items from the Recycle Bin using the Get-PnPRecycleBinItem command of PowerShell as a code snippet below.
  1. $items= Get-PnPRecycleBinItem | ? -Property DeletedByEmail -EQ $UserEmailWhoDeletedFiles  
The above code will get all Recycle bin items deleted by the user since we have applied a Filter with Property “DeletedByEmail” and stored into the $items variable.
Now apply For Each to read all the items one by one in the below snippet
  1. $items= Get-PnPRecycleBinItem | ? -Property DeletedByEmail -EQ $UserEmailWhoDeletedFiles  
  3. foreach($item in $items)  
  5. {  
  7. }  
You can use other Properties to filter like DeletedDate, ItemType, LeafName, DirName etc. As an example get all the List Items only from Recycle Bin as below:
  1. $items= Get-PnPRecycleBinItem | ? -Property ItemType -EQ “ListItem”  
Restore items from Recycle Bin
SharePointPnpPowerShellOnline Module of PowerShell has made a developer's life easier. To restore an Item or file from Recycle Bin use the code snippet as below.
Please find all the parameters associated with Restore-PnpRecycleBinItem
  1. Restore-PnpRecycleBinItem -Identity $item -Force 
Here I have applied -Force command so every Item restore does not ask you for Confirmation.
Complete PowerShell Script
  1. #Powershell script to get Files from SharePoint Online Document Library and Restore them  
  2. #Created By: Vinit Kumar  
  3. # Variable - Change the parameter as it need  
  4. $O365ServiceAccount = ""  
  5. # Your Service Account Name  
  6. $O365ServiceAccountPwd = "Test123$"  
  7. #Your Service Account Password  
  8. $SharePointSiteURL = ""  
  9. # Change this SharePoint Site URL  
  10. $UserEmailWhoDeletedFiles = ""  
  11. # Change the User Name who deleted Files  
  12. #Ends[SecureString] $SecurePass = ConvertTo - SecureString $O365ServiceAccountPwd - AsPlainText - Force[System.Management.Automation.PSCredential] $PSCredentials = New - Object System.Management.Automation.PSCredential($O365ServiceAccount, $SecurePass)  
  13. #Connecting to SharePointDocumentationCentre site  
  14. Connect - PnPOnline - Url $SharePointSiteURL - Credentials $PSCredentials  
  15. #Get all items Deleted by UserEmail Address  
  16. $items = Get - PnPRecycleBinItem | ? -Property DeletedByEmail - EQ $UserEmailWhoDeletedFiles  
  17. foreach($item in $items) {  
  18.     Restore - PnpRecycleBinItem - Identity $item - Force  
  19. }  
Restored Items deleted by user
You can see in the below image three Image files got restored to the Destination site
In this blog, we have talked about how to Get the Recycle Bin items, apply a  filter on those items and restore using SharePoint Online PnP PowerShell Script. I hope this blog helps some SharePoint support person with easy restoration of files.