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:
- Site collection administrator
- User name who deleted files
- Site collection url
- 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:
- Takes all site collection Recycle bin items and assign it into an array variable
- 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)
- 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)
- Checks if deleted item is equal to timestamp
- Checks the user you entered in input
- Creates CSV report with sorted and deleted by the user items
- 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