Super late reply but give this a go:
# Install the PnP PowerShell module
$module = Install-Module PnP.PowerShell -Force
Write-Host "PnP PowerShell version installed: $($module.Version)"
# Disconnect any existing connection
try { Disconnect-PnPOnline } catch { }
# Try to connect to SharePoint
try {
Write-Host "Attempting to connect to SharePoint..."
try {
# First attempt - modern authentication
Connect-PnPOnline -Url "https://carltondigbyschool.sharepoint.com/sites/CDSStaff" -Interactive
} catch {
Write-Host "Modern auth failed, trying alternative method..." -ForegroundColor Yellow
Connect-PnPOnline -Url "https://carltondigbyschool.sharepoint.com/sites/CDSStaff" -UseWebLogin
}
# Test the connection
$web = Get-PnPWeb
Write-Host "Connected to site: $($web.Title)" -ForegroundColor Green
$siteUrl = "https://carltondigbyschool.sharepoint.com/sites/CDSStaff"
$deletionDate = "2025-01-03" # Change this to your target date
$targetDate = [DateTime]::ParseExact($deletionDate, "yyyy-MM-dd", $null).Date
Write-Host "Retrieving recycle bin items..."
$recycledItems = @()
$batchSize = 100
$firstRow = 0
do {
Write-Host "Retrieving items $firstRow to $($firstRow + $batchSize)..." -ForegroundColor Yellow
$batch = Get-PnPRecycleBinItem -RowLimit $batchSize | Select-Object -Skip $firstRow -First $batchSize
if ($batch) {
$filteredBatch = $batch | Where-Object { $_.DeletedDate.Date -eq $targetDate }
$recycledItems += $filteredBatch
Write-Host "Processed $($firstRow + $batch.Count) items..." -ForegroundColor Yellow
}
$firstRow += $batchSize
Start-Sleep -Milliseconds 500
} while ($batch -and $batch.Count -eq $batchSize)
Write-Host "Found $($recycledItems.Count) items deleted on $deletionDate"
$recycledItems | ForEach-Object {
Write-Host "- $($_.Title) (Deleted by: $($_.DeletedByEmail))"
Write-Host " ID: $($_.Id)"
}
if ($recycledItems.Count -gt 0) {
$confirmation = Read-Host "Do you want to restore these items? (Y/N)"
if ($confirmation -eq 'Y') {
foreach ($item in $recycledItems) {
try {
Write-Host "Attempting to restore: $($item.Title)" -ForegroundColor Yellow
Write-Host "Item ID: $($item.Id)" -ForegroundColor Yellow
$restEndpoint = "$siteUrl/_api/web/RecycleBin('$($item.Id)')/Restore()"
Write-Host "Using REST endpoint: $restEndpoint" -ForegroundColor Yellow
# REST call using correct parameter
$response = Invoke-PnPSPRestMethod -Method Post -Url $restEndpoint -ContentType "application/json;odata=verbose"
Write-Host "Successfully restored: $($item.Title)" -ForegroundColor Green
Start-Sleep -Seconds 1
} catch {
Write-Host "Failed to restore: $($item.Title)" -ForegroundColor Red
Write-Host "Error: $_" -ForegroundColor Red
# Try second stage recycle bin
try {
Write-Host "Trying second stage recycle bin..." -ForegroundColor Yellow
$secondStageEndpoint = "$siteUrl/_api/web/RecycleBin2('$($item.Id)')/Restore()"
$response = Invoke-PnPSPRestMethod -Method Post -Url $secondStageEndpoint -ContentType "application/json;odata=verbose"
Write-Host "Successfully restored from second stage: $($item.Title)" -ForegroundColor Green
} catch {
Write-Host "Failed to restore from second stage recycle bin" -ForegroundColor Red
Write-Host "Error: $_" -ForegroundColor Red
Write-Host "`nPlease try restoring manually:" -ForegroundColor Yellow
Write-Host "1. Go to: $siteUrl" -ForegroundColor Yellow
Write-Host "2. Click the Settings gear icon" -ForegroundColor Yellow
Write-Host "3. Click 'Site contents'" -ForegroundColor Yellow
Write-Host "4. Click 'Recycle bin' in the top right" -ForegroundColor Yellow
Write-Host "5. Find the file: $($item.Title)" -ForegroundColor Yellow
Write-Host "6. Select the file and click 'Restore'" -ForegroundColor Yellow
}
}
}
}
}
} catch {
Write-Host "Error occurred: $_" -ForegroundColor Red
Write-Host "`nTroubleshooting steps:" -ForegroundColor Yellow
Write-Host "1. Try running PowerShell as Administrator"
Write-Host "2. Make sure you have the correct SharePoint URL"
Write-Host "3. Verify you have site collection admin rights"
Write-Host "4. Try restoring manually from SharePoint if the script fails"
} finally {
try { Disconnect-PnPOnline } catch { }
}