Restore deleted files from SharePoint Online

The easiest way to restore files in SharePoint Online is obviously just to restore the deleted files using the web interface as Microsoft recommends.

However, 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 the SharePoint site

Connect-PnPOnline -Url

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. In other words, you need to know from which site the files were deleted from.

List all deleted items in the site “rooms”

Get-PnPRecycleBinItem -FirstStage

List all items deleted by a specific user

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

Restore all files deleted by a specific user

Get-PnPRecycleBinItem -FirstStage | ? DeletedByEmail -eq '' | 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 just means that the deleted file already exists, or a newer file exists.


Leave a Reply

Your email address will not be published. Required fields are marked *