Restore files from SharePoint Online with PowerShell

Need to restore a whole bunch of files from the recycle bin in a SharePoint site?

Easiest way is to just restore the deleted files from the recycle bin like Microsoft recommends.

But that won’t work if you need to restore 1000 plus files. Basically the restore process will just get stuck and you will probably die of age before anything happens.

PowerShell to the rescue! Install the SharePoint Patterns and Practices (PnP) cmdlet

Install-Module SharePointPnPPowerShellOnline

Connect to a SharePoint site Notice the sites/rooms in the URL. You need to connect to an actual site on your SharePoint, not just the root of the site. So in other words you need to know on which site the files were deleted from.

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

List all deleted items in the site “rooms”

Get-PnPRecycleBinItem -FirstStage

List all items deleted by a specific user

Get-PnPRecycleBinItem -FirstStage | ? DeletedByEmail -eq 'example@contoso.com' | Format-Table -AutoSize | Out-File -filepath C:\PSExports\example.txt

Restore all files deleted by a specific user

Get-PnPRecycleBinItem -FirstStage | ? DeletedByEmail -eq 'example@contoso.com' | Restore-PnpRecycleBinItem -Force

The restore process will take a while. In my case I restored about 1000 files in about 20 minutes. Note that you will not get a restore log, but you can of course use the above command to check if files disappear from the recycle bin. If files exist on the server you will get error messages, which is fine. This usually means that the deleted file already exists or a newer file exists.


Sources:

https://lazyadmin.nl/powershell/restore-recycle-bin-sharepoint-online-with-powershell/