I am not responsible for any data loss or any support issues! I had to make this myself to restore over 39,000 deleted files from a user's OneDrive, but needed to restore them in bulk. The GUI interface is painful to use and the SharePointPnPPowerShellOnline
module fails to restore items (Due to the 5,000 item limit on SharePoint Online). I got the specific API calls on restoring items from the recycle bin by monitoring network traffic on a web browser while restoring an item from the GUI.
This script requires a module I created that is nowhere near "production ready" and was only made to make it easier for me to create an authentication token to access Microsoft Graph API resources.
The repository for that module is here (with instructions on how to build it):
https://github.com/Smalls1652/pwsh-graph-connect
You must create an Azure AD app registration for your tenant that has the necessary permissions for SharePoint (https://microsoft.sharepoint-df.com/). I've found it much easier to use delegated permissions and authenticating as yourself.
In this example, we're recovering a bulk number of deleted items done by the 'OneDrive System Account' in a user's OneDrive. You must add yourself as a 'Site Collection Administrator' to the user's OneDrive SharePoint Online Site.
With both the pwsh-graph-connect
and SharePointPnPPowerShellOnline
modules:
Connect-PnPOnline -Url "https://contosocom-my.sharepoint.com/personal/john_contoso_com" -PnPO365ManagementShell
$token = Start-GraphApiConnection -ClientId "App/ClientId" -TenantId "TenantId" -Scopes "https://contosocom-my.sharepoint.com/.default"
$deletedItems = Get-PnPRecycleBinItem -FirstStage -RowLimit 150000
$deletedItemsBySystem = $deletedItems | Where-Object { $PSItem.DeletedByName -eq "System Account" }
.\Invoke-SharepointRecycleBinRestore.ps1 -SiteUri "https://contosocom-my.sharepoint.com/personal/john_contoso_com" -AuthToken $token -RecycleBinItems $deletedItemsBySystem
This can take a long time, depending on the number of files that need to be restored.