In this blog, I will demonstrate how to restore items from the recycle bin of the SharePoint Online Site using SharePoint PnP PowerShell Script.
In my scenario a specific user has deleted 2000 files by mistake. The user was not ready to restore the files from the Recycle Bin. Instead they asked the SharePoint Online Support team to do the Job. Restoring individual files deleted by a specific user might take some effort so we have come up with a PowerShell script solution.
Pre-requisites( Environment Details)
- Windows PowerShell
- SharePointPnpPowerShellOnline Module
- Install-Module SharePointPnPPowerShellOnline
SharePoint Recycle Bin
Variables Explanations in this blog
- $O365ServiceAccount="[email protected]"# Your Service Account Name
- $O365ServiceAccountPwd="Test123$"#Your Service Account Password
- $SharePointSiteURL="https://abc.sharepoint.com/sites/Test" # Change this SharePoint Site URL
- $UserEmailWhoDeletedFiles="[email protected]" # Change the User Email who deleted Files
Here you can see we have provided the password in Plain text which you need to provide if you would want this PowerShell script to run automatically through a Timer Job or Scheduler.
For Manual Execution please use
Get-Credential command to read the User Name and Password from the user input.
Connect to SharePoint Online
Before Restoring the Files to SharePoint Online you should be connecting to the SharePoint Site using the below Snippet.
- [SecureString]$SecurePass = ConvertTo-SecureString $O365ServiceAccountPwd -AsPlainText -Force
-
- [System.Management.Automation.PSCredential]$PSCredentials = New-Object System.Management.Automation.PSCredential($O365ServiceAccount, $SecurePass)
-
- #Connecting to SharePoint Online site
-
- Connect-PnPOnline -Url $SharePointSiteURL -Credentials $PSCredentials
Get Recycle Bin Items Deleted by User
We have to get all items from the Recycle Bin using the
Get-PnPRecycleBinItem command of PowerShell as a code snippet below.
- $items= Get-PnPRecycleBinItem | ? -Property DeletedByEmail -EQ $UserEmailWhoDeletedFiles
The above code will get all Recycle bin items deleted by the user since we have applied a Filter with Property “DeletedByEmail” and stored into the $items variable.
Now apply For Each to read all the items one by one in the below snippet
- $items= Get-PnPRecycleBinItem | ? -Property DeletedByEmail -EQ $UserEmailWhoDeletedFiles
-
- foreach($item in $items)
-
- {
-
- }
You can use other Properties to filter like DeletedDate, ItemType, LeafName, DirName etc. As an example get all the List Items only from Recycle Bin as below:
- $items= Get-PnPRecycleBinItem | ? -Property ItemType -EQ “ListItem”
Restore items from Recycle Bin
SharePointPnpPowerShellOnline Module of PowerShell has made a developer's life easier. To restore an Item or file from Recycle Bin use the code snippet as below.
- Restore-PnpRecycleBinItem -Identity $item -Force
Here I have applied -Force command so every Item restore does not ask you for Confirmation.
Complete PowerShell Script
- #Powershell script to get Files from SharePoint Online Document Library and Restore them
- #Created By: Vinit Kumar
- # Variable - Change the parameter as it need
- $O365ServiceAccount = "[email protected]"
- # Your Service Account Name
- $O365ServiceAccountPwd = "Test123$"
- #Your Service Account Password
- $SharePointSiteURL = "https://abc.sharepoint.com/sites/Test"
- # Change this SharePoint Site URL
- $UserEmailWhoDeletedFiles = "[email protected]"
- # Change the User Name who deleted Files
- #Ends[SecureString] $SecurePass = ConvertTo - SecureString $O365ServiceAccountPwd - AsPlainText - Force[System.Management.Automation.PSCredential] $PSCredentials = New - Object System.Management.Automation.PSCredential($O365ServiceAccount, $SecurePass)
- #Connecting to SharePointDocumentationCentre site
- Connect - PnPOnline - Url $SharePointSiteURL - Credentials $PSCredentials
- #Get all items Deleted by UserEmail Address
- $items = Get - PnPRecycleBinItem | ? -Property DeletedByEmail - EQ $UserEmailWhoDeletedFiles
- foreach($item in $items) {
- Restore - PnpRecycleBinItem - Identity $item - Force
- }
Restored Items deleted by user
You can see in the below image three Image files got restored to the Destination site
In this blog, we have talked about how to Get the Recycle Bin items, apply a filter on those items and restore using SharePoint Online PnP PowerShell Script. I hope this blog helps some SharePoint support person with easy restoration of files.