SharePoint PowerShell Restore Recycle Bin

I would not identify myself as a programmer or developer but I certainly enjoy technology and writing scripts to solve problems that I encounter. I am pretty comfortable in my Microsoft Excel formulas and my ability to diagnose problems. I understand the logic that needs to happen but not the syntax to make it happen.

Earlier today I had an employee accidentally delete some 40,000 objects from my SharePoint Document Library that is shared company-wide. The browser tools with SharePoint Online (2016) only gave me the ability to restore a few files or folders at a time. I decided to try to solve my problem with PowerShell. I was not that familiar with the PnP PowerShell tools but they helped me solve my problem:

First, I launched PowerShell in administrator mode and installed the PowerShell Gallery:

Install-Module SharePointPnPPowerShellOnline

Second, I got connected to my SharePoint Online:

Connect-PnPOnline -Url https://contoso.sharepoint.com

Third, I wrote a script to see if I could see a count of the files in the recycle bin:

#Count of the Items in the First Stage Recycle Bin
(Get-PnPRecycleBinItem -FirstStage).count

I noticed that it included folders and only wanted to restore the files. SharePoint will restore the folder structure automatically. So I cleaned up my scrip to only include files:

#Show Count of “File” in the First Stage Recycle Bin
(Get-PnPRecycleBinItem -firststage | ? ItemType -like "File").count

Finally, I was able to use my script that would restore the 40,000 files:

#Restore All “File” in the First Stage Recycle Bin
Get-PnPRecycleBinItem -firststage | ? ItemType -like "File" | Restore-PnpRecycleBinItem -Force

There were a few phone calls to consultants that were not of much help. I am posting this online in hopes that it might be helpful to others. Enjoy!