Saturday, July 2, 2016

SharePoint Online: Restore a document from the recycle bin using PowerShell

Task: Recover a deleted document from the recycle bin in SharePoint online.

How to restore from recycle bin in SharePoint online?

  • Login to your SharePoint online site and click "Site Contents" from either site settings gear or from left navigation.
  • Click on "Recycle bin" link from the top of the page.
  • Select the document(s) you would like to restore and then click "Restore Selection".
    sharepoint online restore from recycle bin
  • Confirm restore by clicking "OK" button.
  • The files you selected is available in the original location from where it was deleted.

SharePoint online: restore from recycle bin using PowerShell
Now, lets use PowerShell to restore all excel files from recycle bin. 
#Load SharePoint Online Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
##Variables for Processing
$SiteUrl = ""

#Get the password to connect 
$Password = Read-host -assecurestring "Enter Password for $UserName"
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,$Password)
Try {    
    #Setup the context
    $Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
    $Context.Credentials = $Credentials
    #Get the web recycle bin
    $Web = $Context.web
    $RecycleBinItems = $Web.RecycleBin

    #Get all Excel files from Recycle bin
    $ExcelFiles= $RecycleBinItems | Where {$_.Title -like "*.xlsx"}
    Write-Host "Total Number of Excel Files found Recycle Bin:" $ExcelFiles.Count
    #Restore from Recylce bin
    $ExcelFiles | ForEach-Object { $_.Restore() }
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
Please note that the above script restores from 1st stage recycle bin (or end-user recycle bin). Instead of Web object, you may have to use Site object if you want to restore from Site collection Recycle bin (Or 2nd stage recycle bin).

You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.

Check out these SharePoint products:

No comments :

Post a Comment

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...