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
The script fails if the item of the same name exists in the document library.
ReplyDeleteAny suggestions how can this be overcome?
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.
DeleteHi Aleks,
ReplyDeleteThis 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
Hi Ashwin,
DeleteTry 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"
Thanks for the prompt response.
DeleteI 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.
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:
Delete"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.
@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@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"
DeleteThanks for the script Aleksandr, it's of great help.
Hi, the user name in input should be name.surname
DeleteI fixed this by correcting the user name. I tried the full email instead of just the user name.
DeleteDoesn't work with Sharepoint 2010 as Microsoft.SharePoint.Client.UserProfiles.dll doesn't exist.
ReplyDeleteHi Aleks,
ReplyDeletethank you very much for this script.
It works like a charm.
Alex
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?
ReplyDeleteI am trying to restore files deleted by a user between two dates from a 0365 Group Site
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.
DeleteThis comment has been removed by the author.
ReplyDeleteDid anyone tried the script on Sharepoint Online?
ReplyDeleteIt 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?
Hi, yes, it can be slow.
DeleteI 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.
ReplyDeleteif ( $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
}
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.
ReplyDeleteAny thoughts?
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