SharePoint PnP PowerShell script for restoring a lot! of deleted items from Recycle Bin

Koen Stronkhorst 0 Reputation points
2024-08-07T12:22:33.4733333+00:00

Hi there!

One of the users of a customer had, by accident, deleted all files from a specific SharePoint. I was able to retrieve these files in the Recycle Bin. I know how to restore these files manually, but this user had deleted all items (again by accident), and it contains thousands of files.

I didn't want to restore all of these files by hand, so I've found which I thought it would help me restore the files automatically. This script worked just fine in two of my test tenants, but although the script gives me no errors when running it for the customer's tenant, no items deleted by this specific user and/or on this specific date are being restored. To be sure, I've added the script I've used below:

PowerShell script to restore SharePoint items from the recycle bin for a specific date and user:

# Set the date for items to be restored 
$specificDate = Get-Date -Date "01-08-2024" -Format "dd-MM-yyyy"  
# Set the email address of the user whose items need to be restored 
$userEmail = ""  
# Connect to the SharePoint site 
Connect-PnPOnline -Url "https://_____-portal1.sharepoint.com/" -Interactive  
# Get all items that were deleted on the specific date and by the specific user 
$itemsToRestore = Get-PnPRecycleBinItem | Where-Object {     
$_.DeletedDate.Date -eq $specificDate.Date -and     
$_.DeletedByEmail -eq $userEmail 
}  
# Restore each item 
foreach ($item in $itemsToRestore) {
 *Get-PnPRecycleBinitem -RowLimit 3000 | -Restore-PnPRecycleBinItem -Force* 
}  
# Print a success message 
Write-Host "Items deleted by $userEmail on $specificDate have been restored."

Keep in mind that this specific SharePoint is a very old SharePoint (portal SharePoint) and still has an old structure to it, compared to newer team site. Not sure if this could matter, but good to know.

Hopefully somebody is able to help me out, would love to know what I could be doing wrong, or what tweaks I might have to add to my PowerShell script.

Thanks in advance!

Microsoft 365 and Office SharePoint Development
Windows for business Windows Server User experience PowerShell
{count} votes

2 answers

Sort by: Most helpful
  1. Anonymous
    2024-08-08T08:16:29.4966667+00:00

    Please try following PowerShell to restore files by specific date and user.

    Note: Go to the recycle bin, check the delete date and delete user, then replace data in the PowerShell.

    $date1=Get-date("08/08/2024") -Format "MM-dd-yyyy"
    Connect-PnPOnline https://tenant.sharepoint.com/sites/test -Interactive
    Get-PnPRecycleBinItem | ? {(($_.DeletedDate).Date -eq $date1) -and ($_.DeletedByEmail -eq '******@tenant.OnMicrosoft.com')} | Restore-PnpRecycleBinItem -Force
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

  2. Alvaro Avila 170 Reputation points
    2024-09-09T13:53:53.5466667+00:00

    Hi Koen,

    PnP Community has this sample that might help you https://pnp.github.io/script-samples/bulk-restore-from-recyclebin/README.html?tabs=pnpps

    Additionally, this is a free and open-source app that can help you: NovaPoint

    This is the doc about restoration of recycle bin: Restore items from the recycle bin

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.