Showing posts with label PowerShell script to restore files. Show all posts
Showing posts with label PowerShell script to restore files. Show all posts

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