Teams Files uses a SharePoint Recycle bin, because it stores files in a Library, but the web interface to manage the recycle bin isn’t very helpful. If someone by mistake or perhaps trying to delete a folder they synced, have deleted a lot of files, it is a nightmare to restore using the web interface. I don’t think your browser will handle it very well either.
So I searched the Internet for a script to restore or recover files from a SharePoint Online Recycle bin, and came upon SharepointDiary.com describing the options well.
Except it was written in 2019 and I had to user interactive authentication due to MFA, so I added -Interactive to the Connect-PnPOnline command. But I kept getting Access Denied when trying to get-pnprecyclebinitems, although I had all permission in the world. So I did another dive in the Internet and found a thread saying we need to capture the connection and use it specifically when sending commands.
And so I added $conn to capture and use through out the script like this:
#Parameter
$SiteURL= "https://dbinsp.sharepoint.com/sites/YourSite"
#Connect to PnP Online
#Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)
$conn = Connect-PnPOnline -Url $SiteURL -Interactive
$Web = Get-PnPWeb
(Get-PnPRecycleBinItem -Connection $conn).count
#Get All Items Deleted in the Past 2 Days
$DeletedItems = Get-PnPRecycleBinItem -Connection $conn -RowLimit 500000 | Where { $_.DeletedDate -gt (Get-Date).AddDays(-2) }
#To edit the deleted files list
#$DeletedItems | Export-Csv Deleteditems.csv
#$DeletedItems | Import-Csv Deleteditems.csv
#Restore Recycle bin items matching given query
$DeletedItems | Restore-PnpRecycleBinItem -Connection $conn -RowLimit 500000 -Force
I still got an error message saying the request uses too many resources, and was only restoring about 1200 files of Office documents. Anyway it kept on and once it finished after about 10 minutes, all files where recovered. No longer visual in the recycle bin and user could instantly verify all files where back in the original place.

If it is a large set of files, consider turning off the option to sync the library. It does create a couple emergencies when one accidentally synchronizes a library or wants to stop synchronization, but fails to remove the library from synchronization. And instead deletes files and folders, which synchronizes a delete command to the library.


We can remove the sync button from the Library settings — Advanced Settings:

We can also disable it for all Sites: Go to Site Settings >> Click on “Search and Offline Availability” under the “Search” group and Set “No” for the “Offline Client Availability” setting.
Or using PowerShell:
#Parameters
$SiteURL = "Your Site URL"
$ListName = "Your List Name"
#Connect to PnP
$conn = Connect-PnPOnline -url $SiteURL -Interactive
#Get the Library
$List = Get-PnPList -Connection $conn $ListName
#Exclude List or Library from Sync
$List.ExcludeFromOfflineClient = $true
$List.Update()
Invoke-PnPQuery
If you haven’t already, you would also need to grant consent for the PnP Management Shell Enterprise Application, which you can request through first time interactive connection or if you have the necessary permission approve during first time interactive connection.
Leave a Reply
You must be logged in to post a comment.