Last active
December 21, 2023 15:14
-
-
Save pmatthews05/bf918f76f8fd06f3b0629327d246317a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[CmdletBinding(SupportsShouldProcess)] | |
param( | |
# The URL of the Sitecollection where the recycle bin is. | |
[Parameter(Mandatory)] | |
[string] | |
$SiteUrl, | |
# Full Path of CSV file of Get-AllRecycleBin.ps1 | |
[Parameter(Mandatory)] | |
[string] | |
$Path | |
) | |
function Restore-RecycleBinItem { | |
param( | |
[Parameter(Mandatory)] | |
[String] | |
$Id | |
) | |
$siteUrl = (Get-PnPSite).Url | |
$apiCall = $siteUrl + "/_api/site/RecycleBin/RestoreByIds" | |
$body = "{""ids"":[""$Id""]}" | |
Write-Verbose "Performing API Call to Restore item from RecycleBin..." | |
try { | |
Invoke-PnPSPRestMethod -Method Post -Url $apiCall -Content $body | Out-Null | |
} | |
catch { | |
Write-Error "Unable to Restore ID {$Id}" | |
} | |
} | |
$ErrorActionPreference = 'Continue' | |
$InformationPreference = 'Continue' | |
Connect-PnPOnline -Url:$SiteUrl -UseWebLogin | |
@($(Import-Csv -Path:"$Path")).ForEach({ | |
$csv = $PSItem | |
Write-Information -MessageData:"Restore item $($csv.Title)" | |
Restore-RecycleBinItem -Id $($csv.ID) | |
}) |
This was very useful thank you.
I am currently restoring ~150,000 items. I've used Co-Pilot to add a counter to the my script. You may find it useful
# Get the total number of rows in the CSV file
$TotalRows = (Import-Csv -Path:"$Path").Count
# Initialize a counter for the current row
$CSVRow = 0
# Loop through each row in the CSV file
@($(Import-Csv -Path:"$Path")).ForEach({
$csv = $PSItem
# Increment the counter by one
$CSVRow++
# Write the information message with the desired format
Write-Information -MessageData:"Restoring item $CSVRow of $TotalRows. Item $($csv.Title)"
# Restore the recycle bin item with the given ID
Restore-RecycleBinItem -Id $($csv.ID)
})
Hi @SharkEyes93 thank you for your suggestion.
There is a newer better version of this script which can be found at PNP Script Samples. It batches and runs a bit quicker.
https://pnp.github.io/script-samples/bulk-restore-from-recyclebin/README.html?tabs=pnpps
Feel free to Co-pilot the script and improve on it, I think you will get a PNP PowerShell script badge if you make any updates that get accepted into the Pull Request.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sorry @ZeeGy-net, I've just seen this comment, and noticed you too told me that my variable was incorrect. Thank you to you too.