Undelete items from SharePoint Recycle bin
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.
# 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."
}
}
Check out the PnP PowerShell to learn more at: https://aka.ms/pnp/powershell
The way you login into PnP PowerShell has changed please read PnP Management Shell EntraID app is deleted : what should I do ?
Contributors
Author(s) |
---|
Dipen Shah |
Adam Wójcik |
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.