GitHub Icon Image

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.

  • PnP PowerShell
  • CLI for Microsoft 365

# 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

# aim of this script is to restore items which were deleted by specific user

$siteURL = "https://tenant.sharepoint.com/sites/Dataverse"
$userEmailAddress = "user@tenant.onmicrosoft.com"

$m365Status = m365 status
if ($m365Status -match "Logged Out") {
    m365 login
}

$deletedItems = m365 spo site recyclebinitem list --siteUrl $siteURL --query "[?DeletedByEmail == '$userEmailAddress']" | ConvertFrom-Json
$deletedItemsIdList = [String]::Join(',', $deletedItems.Id)

Write-Host "Restoring is in progress for Items: $deletedItemsIdList"
m365 spo site recyclebinitem restore --siteUrl $siteURL --ids $deletedItemsIdList
Write-Host "Done"

Check out the CLI for Microsoft 365 to learn more at: https://aka.ms/cli-m365

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.

Back to top Script Samples
Generated by DocFX with Material UI