Data

Undelete items from SharePoint Recycle bin

This script allows users to undelete items from recycle bin and restore it in respective document library and list.

Summary

sometimes users need to restore items from SharePoint recycle bin. This script allows them to undelete items from recycle bin and restore it in respective document library and list.

powershell

# Make sure necessary modules are installed
# PnP PowerShell to get access to M365 tenent

Install-Module PnP.PowerShell
$siteURL = "https://tenent.sharepoint.com/sites/Dataverse"
$rows = 10000 
$userEmailAddress = "user@tenent.onmicrosoft.com" #admin user
#  -UseWebLogin used for 2 factor Auth.  You can remove if you don't have MFA turned on
Connect-PnPOnline -Url  $siteUrl
 $deletedItems = $null
 # Get files which is deleted by specific user.
 $deletedItems = Get-PnPRecycleBinItem -FirstStage -RowLimit $rows | Where-Object {$_.DeletedByEmail -Eq $userEmailAddress} | select Id,Title,LeafName,ItemType
 if($deletedItems.Count -gt 0)
 {
    Foreach ($deletedItem in $deletedItems){
        Write-Host "Restoring is in process for Item Id : " $deletedItem.Id
        Restore-PnPRecycleBinItem -Identity $deletedItem.Id.ToString() -Force
        Write-Host "Item with Id : " $deletedItem.Id " has been restored successfully."
    }
 }

Contributors

Disclaimer

THESE SAMPLES ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.

Tool documentation & references

Keep exploring