young bearded man feeling extremely shocked and surprised, anxious and panicking, with a stressed and horrified look and working with a laptop
Shocked guy

How to restore SharePoint files with PowerShell

Shit happens — sometimes we are not that concentrated when we are working. Somebody calls us without an appointment, and we were cleaning up our document libraries and by mistake we delete multiple files from SharePoint libraries. In this article, you will learn, how to restore SharePoint files from the diverse recycle bins in SharePoint. Like every time you will learn it for two ways: manually and automated. These instructions do also work for SharePoint items.

Restore SharePoint files manually from the first-stage recycle bin

Let’s assume, that you have a library with two documents in it.

Screenshot of the document library Opportunitylibrary

One unfocused moment, and you have deleted the file Opportunity2.docx without intention.

Screenshot of SharePoint library, where the document have been removed by accident

As a site member, you have in fact the possibility to check your recycle bin to restore the file. In order to do this, click on the settings gear in the upper right corner and click on site contents.

SharePoint settings gear
Settings contents setting

Now click on Recycle Bin.

Screenshot of link to the recycle bin

As you can see, I can see the removed file from John dodo, which is my test user with member access. Now mark the file, you want to restore and click on Restore.

Screenshot of restoring the file from the SharePoint recycle bin

Thats it, your file is back at the place, where you have deleted it:

Screenshot of a SharePoint library, where the opportunity2.docx could be restored succesfully

If you cannot see the file here, ask your Site collection administrator, to check the second-stage recycle bin (site collection recycle bin), which’s content will be retained for 93 days, unless the limit of the site collection storage is not exceeded or the item have not been removed manually by a site collection administrator. In the following, I will describe how to restore from the second-stage recycle bin.

Restore SharePoint files manually from the second-stage recycle bin

Now we assume, that we have removed the file opportunity2.docx accidentially, but we need to restore it. We already have checked the site recycle bin, but we could not find the file in it.

As you see, there is no opportunity2.docx file:

Screenshot of a SharePoint site recycle bin, where the file opportunit2.docx could not be found.

In this case, we need someone, who has the site collection administrator role, because we have to check the site collection settings. In the view as a member, the site collection settings look like this — so there is no option to visit the site collection recycle bin and therefore no option to restore SharePoint files.

Screenshot of the site settings from a user, which has no site owner permission

In order to show you the site settings as an owner, I have promoted John Dodo to Site Owner:

Screenshot of group membership permissions

As you can see, I cannot see more as a site owner:

Screenshot of site settings as a site owner

Therefore, I will switch the user to one with site collection admin rights and we will see way more settings:

Screenshot of site settings as a site collection admin

Maybe you have noticed, that you can see Recycle Bin setting below Site collection Administration

Screenshot of the recycle bin setting in the site settings

This is the place, where we can see the files, which are removed, but also can be restored from users without the site collection administrator permission. As you can see, there is a hint to the second-stage recycle bin. Click on the hint.

Screenshot of first stage recycle bin, with the hint to the second stage recycle bin

This is the place, where we can see the files, which are in the second-stage recycle bin. We should see the opportunity2.docx file here:

Screenshot of the second stage recycle bin

Restore SharePoint files automatically from the second-stage recycle bin

Prerequisites

The prerequisite is, that you have to use a user, which has site collection administrator permissions.

Screenshot of site collection administrators

Description

Previously we saw, that you can restore files easily by hand, but imagine, that you have removed more than 100 files, this might be a bit clicky time-consuming. So let’s check what we can do with PNP PowerShell.

I am using John Dodo, who is currently a site collection administrator. John Dodo has removed more than 100 files by accident and I want to restore SharePoint files removed by John Dodo. This is currently not possible in GUI.

Screenshot of removed items in the recycle bin

In order to restore SharePoint files, I am using the PowerShell Module PNP.PowerShell.

I am connecting to the sales site. If you feel unsafe with connecting, check out the post Connect to SharePoint with PowerShell | SharePoint Online (sposcripts.com)

Connect-PnPOnline https://devmodernworkplace.sharepoint.com/sites/Sales/ -Interactive

After connecting, I can see the items in the recycle bin. I have removed more than 100 files. You can do it with the cmdlet:

Get-PNPRecycleBinItem
Screenshot of the removed files in PowerShell

When you pass the ID, you can also get seperate recycle bin items:


Get-PnPRecycleBinItem -Identity "eaf270e4-08f0-41b3-a424-d804c6f33a16"

If you pipe the Item, you can directly restore it:

Get-PnPRecycleBinItem  -Identity "eaf270e4-08f0-41b3-a424-d804c6f33a16" | Restore-PnPRecycleBinItem -Force
Screenshot of a specific item in the recycle bin in PowerShell

I want to restore only the files, from John Dodo, so I am doing the restore with the out grid view cmdlet:

Get-PnPRecycleBinItem  |
    Select-Object Title, ID, AuthorEmail, DeletedbyEmail, DeletedDate, DirName | 
        Out-GridView -PassThru |
            ForEach-Object { Restore-PnPRecycleBinItem -Identity $_.Id.Guid -Force}

Now you can see the grid, where you can choose John Dodo as the deletor:

Screenshot of the out gridview output, which shows all the items in the recycle bin

I add a filter criteria:

Screenshot of the out gridview output, which shows the SharePoint files, with filtering options

Now I can add John Dodos mailaddress and with CTRL + A you can mark all entries and confirm with ok

Screenshot of the out gridview output, which shows the SharePoint files, before the restore filtered on John.Dodo

After doing this, the files, which have been by John Dodo are restored!

Screenshot of the recycle bin after the restore of SharePoint files

Troubleshooting

If you experience this issue, your permission is not sufficient for the site:

Get-PnPRecycleBinItem : Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
At line:1 char:1
+ Get-PnPRecycleBinItem
+ ~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (:) [Get-PnPRecycleBinItem], ServerUnauthorizedAccessException
    + FullyQualifiedErrorId : EXCEPTION,PnP.PowerShell.Commands.RecycleBin.GetRecycleBinItems

If you encounter this issue, a file with the name already exists at the location, where it was. Rename the file in the library or remove it, to restore the file.

Restore-PnPRecycleBinItem : A file with this name "Document.docx" already exists in "sites/Sales/Invoices". To restore the file, rename the existing file and try again.
At line:17 char:30
+ ... rEach-Object { Restore-PnPRecycleBinItem -Identity $_.Id.Guid -Force}
+                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (:) [Restore-PnPRecycleBinItem], ServerException
    + FullyQualifiedErrorId : EXCEPTION,PnP.PowerShell.Commands.RecycleBin.RestoreRecycleBinItem

If this error occurs, it is because you have stressed the API.

Get-PnPRecycleBinItem : The request uses too many resources.
At line:1 char:1
+ Get-PnPRecycleBinItem | Select-Object Title, ID, AuthorEmail, Deleted ...
+ ~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (:) [Get-PnPRecycleBinItem], ServerException
    + FullyQualifiedErrorId : EXCEPTION,PnP.PowerShell.Commands.RecycleBin.GetRecycleBinItems

Retry your cmdlet and if it is still problematic, add -rowlimit 5000, so you will see the first 5000 items.

If you add rowlimit to your code, it will look like this:

Get-PnPRecycleBinItem -RowLimit 5000 |
    Select-Object Title, ID, AuthorEmail, DeletedbyEmail, DeletedDate, DirName | 
        Out-GridView -PassThru |
            ForEach-Object { Restore-PnPRecycleBinItem -Identity $_.Id.Guid -Force}

BONUS: Ready-to-run PowerShell script

param
(
    [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   Position=0)]
     $SiteUrl
)


Connect-PnPOnline -Url $SiteUrl -Interactive -ErrorAction Stop


Get-PnPRecycleBinItem  |
    Select-Object Title, ID, AuthorEmail, DeletedbyEmail, DeletedDate, DirName | 
        Out-GridView -PassThru |
            ForEach-Object { Restore-PnPRecycleBinItem -Identity $_.Id.Guid -Force}

Further Reading

Out-Gridview gives us many possibilities to filter objects interactively, check out the blog post to learn more: How to filter for PowerShell objects easily | SPO Scripts

Here you can find the official reference of Microsoft regarding the recycle bin: Manage the Recycle bin of a SharePoint site (microsoft.com)

References

Cover picture:

Haus Foto erstellt von kues1 – de.freepik.com

Leave a Reply