I'm working on a general function to recover an item programatically from a recycle bin via Powershell and CSOM. This is what I have so far, it doesn't throw an error, but it doesn't recover the file either. test_word.docx is a word document I created and deleted, it does find it and output test_word.docx restored, but the file sits in the recycle bin still.
Am I missing something here?
$FileName = "test_word.docx"
$context = New-Object Microsoft.SharePoint.Client.ClientContext("https://tenant-my.sharepoint.com/personal/eric_alexander_domain_com")
$context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($credential.UserName, $credential.Password)
$site = $context.Site
$recycleBinCollection = $site.RecycleBin
$context.Load($site)
$context.Load($recycleBinCollection)
$context.ExecuteQuery()
foreach($item in $recycleBinCollection){
if($item.LeafName -eq $FileName){
$item.Restore()
Write-Host "$FileName restored"
return
}
}