Friday, September 11, 2015

SharePoint Online: How To restore deleted items from Recycle bin with PowerShell.

If a SharePoint online user has deleted a huge amount of folders and files, he had in Document Library by chance, you can write a powershell script to restore it, instead do it manaully.

The script I have written can be fouund above on GitHub.


GitHub:

The script takes the following params:
  1.      Site collection administrator
  2.  User name who deleted files
  3.    Site collection url
  4.     Timestamp of items deleted date (ex., strart 2015-09-03 00:00:00 and end date 2015-09-03 23:59:59)



The script does the following actions:

  1. Takes  all site collection Recycle bin items and assign it into an array variable
  2.  It sorts an array in descending using merge method (https://gallery.technet.microsoft.com/scriptcenter/Merge-Sort-for-PowerShell-e5fdf3ab) (the first record in the last deleted)
  3. Limit deleted items count to the start date you enter in the script (ex. If recycle bin contains 8000 items from 2015-06-01 till 2015-09-09 and you want to restore items deleted for one particular day 2015-09-03, the items count will be limited to the start date)
  4.  Checks if deleted item is equal to timestamp
  5. Checks the user you entered in input
  6. Creates CSV report with sorted and deleted by the user items
  7.   Restores deleted items one by one from $csvReport filtered and sorted array with following methods

                                     $_.DeletedItem.Restore()
                                     $ctx.ExecuteQuery()
NOTE

      i you wish to get a report first to see what items will be restored first, just you need to comment the line with


                          $_.DeletedItem.Restore()
                           $ctx.ExecuteQuery()

and look at report.csv file in c:\tmp folder


The powershell script was created for a particular case, when the user deleted 25 GB of files stored on local drive via OneDrive and files were synhronized with SharePoint library. Files were deleted extremly fast and in one mass transaction. In this case, it was very important to get data in right order and hierarhy, as a Document libary has a lot of  nested folders and files . So you can takes the script as an example and modify it for your own case and a needs.

 Here a small example of Document library folder structure:





So have a nice day, J
I hope it will help somebody
Best regards,
Aleks

20 comments:

  1. The script fails if the item of the same name exists in the document library.

    Any suggestions how can this be overcome?

    ReplyDelete
    Replies
    1. Hi, this is SharePoint restoring works by design, if the folder or items already exist, you need to rename or copy somewhere else (manually) it in document library, and the restore it.

      Delete
  2. Hi Aleks,
    This script is a life saver. However it is not restoring.
    It creates a csv file with junk characters 
    However on Screen it correctly list the total number of deleted files
    Limited Array items: 15

    Count DirName
    ----- -------
    15 TechTeam/ClientsTesting/Documentation

    Any thoughts ?

    Ashwin

    ReplyDelete
    Replies
    1. Hi Ashwin,
      Try to change the date

      I suppose, the Recycle Bin file date range is not reached between your dates. As date's range can be bigger, try to make date range bigger between start and end dates.

      #$startDate = Read-Host -Prompt "Enter start date in format YYYY-MM-DD"
      $startDate = "2015-09-03 00:00:00"
      #$endDate = Read-Host -Prompt "Enter end date in format YYYY-MM-DD"
      $endDate = "2015-09-04 23:59:59"

      Delete
    2. Thanks for the prompt response.
      I did that my files were deleted July 12th 2017
      I used your default start date & set the end date to 2017-12-12.

      I (layman) suspect that the answer lies in why it is listing the correct number of files , but the csv file only has one cell A1 populated with junk characters.

      Delete
    3. I am having trouble with this as well. It is outputting a csv file with junk characters. After specifying the user name it outputs this:
      "Array items: 4
      Iterations: 4
      0
      1
      2
      3
      Limited Array items: 4
      Done"

      Only three of the four items in the recycle bin were deleted by the user specified.

      Delete
    4. @Ashwin - The issue occurs due to entering the username as "user@domain.com" when it prompts for the user who deleted the files. You need only enter the alias, (E.G. EmanuelS instead of EmanuelS@domain.com)

      Delete
    5. @Ashwin - The issue is encountered when the format for the username who deleted the files is incorrect or is not the user who deleted them. For example, instead of putting "EmanuelS@domain.com", you need only put "EmanuelS"

      Thanks for the script Aleksandr, it's of great help.

      Delete
    6. Hi, the user name in input should be name.surname

      Delete
    7. I fixed this by correcting the user name. I tried the full email instead of just the user name.

      Delete
  3. Doesn't work with Sharepoint 2010 as Microsoft.SharePoint.Client.UserProfiles.dll doesn't exist.

    ReplyDelete
  4. Hi Aleks,

    thank you very much for this script.
    It works like a charm.

    Alex

    ReplyDelete
  5. Been looking everywhere for this. Unfortunately for me it creates the spreadsheet with rows of deleted files but doesn't actually restore them.. Any help?
    I am trying to restore files deleted by a user between two dates from a 0365 Group Site

    ReplyDelete
    Replies
    1. Hi Simon, if you have rows in your spreadsheet, then it should restore. this script works for classic SharePoint online sites, I m not tested it for O365 groups site. Check Recycle bin property, try in ste a breakpoint to this line and check $_.DeletedItem.Restore() what happens.

      Delete
  6. This comment has been removed by the author.

    ReplyDelete
  7. Did anyone tried the script on Sharepoint Online?
    It finds the files but it takes forever to generate the report, like a line per second.
    It runs very slow on Sharepoint 365.
    Any ideas why or how to speed it up?

    ReplyDelete
  8. I have no promises but since you dont have to recover folders before files i add this extra check. It just excludes any itemtype that is not file. This seems to have stopped the script exiting out when a folder already exists. Maybe I have got it wrong but seems to work for me.

    if ( $item.ItemType -eq "File"){
    $csvReport += New-Object PSObject -Property $details
    Write-Host "Deleted item:" $item.Title, "by " $atDelimited[0].ToString(), "Deleted Date: " $item.DeletedDate, "Location: " $item.DirName, "Item type: " $item.ItemType
    }

    ReplyDelete
  9. Aleksandr. Thanks for this very helpful. I am having trouble when the user is Sharpoint\system and it was a system config that deleted the files.
    Any thoughts?

    ReplyDelete
  10. I am getting an error of "The attempted operation is prohibited because it exceeds the list view threshold enforced by the administrator. Any help would be appreciated.

    ReplyDelete