Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ks-realconnections opened this issue Aug 14, 2024 · 0 comments

Comments

@ks-realconnections
Copy link

ks-realconnections commented Aug 14, 2024

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 containts 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 customers'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 = "[email protected]"  

# 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 teamsites. 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! Kind regards,

Koen Stronkhorst

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant