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

foreach($item in $recycleBinCollection){
        if($item.LeafName -eq $FileName){
            Write-Host "$FileName restored"
up vote 13 down vote accepted

You need to add $context.ExecuteQuery() at the end. Without it operations won't be executed on the server.

  • 4
    I knew I was doing something dumb, thanks – Eric Alexander Aug 28 '15 at 18:40

You need to add $context.ExecuteQuery() also make sure you don't restore more than 30 files in a single ExecuteQuery otherwise you will get errors doing so.
Microsoft limited the Restore possibility to 30 files max (also timeouts might generate other errors).
That's why I always restore 20 by 20.

Tag the items to Restore or Delete

# Only Restore Files
if($recycleBinItem.ItemType -eq [Microsoft.SharePoint.Client.RecycleBinItemType]::File)
    $fullPath = $recycleBinItem.DirName
    $fullPath += "/"
    $fullPath += $recycleBinItem.Title
    "$fullPath will be restored"
# Delete Folders from RecycleBin
elseif($recycleBinItem.ItemType -eq [Microsoft.SharePoint.Client.RecycleBinItemType]::Folder)
    $fullPath = $recycleBinItem.DirName
    "$fullPath will be removed"

Execute the modifications within the loop

if($countModifications -ge 20)
    "Executing $countModifications modifications"
    "$countModifications modifications executed"
    $countModifications = 0;

Execute the modifications after the loop

if($countModifications -gt 0)
    "Remaining $countModifications modifications"
    "Remaining $countModifications modifications executed"
    $countModifications = 0;

"$countTotalModifications modifications executed in total"







  • I have been able to restore 50 to 100 items at once with csom – Dinerdo Oct 27 '17 at 17:27
  • @Dinardo were you on SharePoint Online ? I got an answer from Microsoft directly telling me this was a hardcoded limitation provided for all SPOnline tenants. – Hybris95 Oct 30 '17 at 8:05
  • You're right. I missed the online tag. – Dinerdo Oct 30 '17 at 12:09

Your Answer


By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.