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.
- Install-Module SharePointPnPPowerShellOnline
SharePoint Recycle Bin
Variables Explanations in this blog
- $O365ServiceAccount="[email protected]"# Your Service Account Name
- $O365ServiceAccountPwd="Test123$"#Your Service Account Password
- $SharePointSiteURL="https://abc.sharepoint.com/sites/Test" # Change this SharePoint Site URL
- $UserEmailWhoDeletedFiles="[email protected]" # 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.
- [SecureString]$SecurePass = ConvertTo-SecureString $O365ServiceAccountPwd -AsPlainText -Force
- [System.Management.Automation.PSCredential]$PSCredentials = New-Object System.Management.Automation.PSCredential($O365ServiceAccount, $SecurePass)
- #Connecting to SharePoint Online site
- 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.
- $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.


Ferdie BothaPosted Jan 25, 2021, 5:47 PM
This is excellent! How to specify additional parameter of date range for the files to be recovered please?
Vinodh NarayananPosted Jul 26, 2020, 2:19 AM
Good one. But format the ps script in editor before post your blog 😊
Roshan RathodPosted Jul 24, 2020, 10:08 AM
good one..keep it up sir
Sandeep MishraPosted Jul 22, 2020, 6:25 AM
Very Useful