Learn how to retrieve deleted items with PnP PowerShell, filter the recycle bin, and restore them
The challenge
If you have a site with a lot of activity, your have probably tried that a user have deleted an item/file that they were not supposed to.
There is a recycle bin available for your site, but the filter / search options are limited.
I have sites with 50+ users, and between 100-300 files are deleted everyday (on purpose with automations), but if a user has deleted an item by accident, it can be hard to locate that specific item/file.
Solution
We can use PowerShell 7 and the PnP PowerShell scripts to collect the deleted items, and thereafter filter the list
If you have not all ready installed PnP PowerShell, your should do that by following this guide
Connect to your site
First we need to connect to our site by writing the following in PowerShell 7
connect-PnPOnline https://YOURTENANT.sharepoint.com/sites/YOURSITENAME -interactive
If you don’t get any error after you have logged in, you are now connected to your site
Getting the Recycle Bin list
To get the list of deleted items, we are going to use the script that is documented here
Your can run this command directly
Get-PnPRecycleBinItem
Or if you have many items, you can set a row limit by writing
Get-PnPRecycleBinItem -RowLimit 1000
Here is (a part of) the results from my site
Filtering your list
From the recycle bin we are getting the following values returned
- Title
- Id
- ItemType
- LeafName
- DirName
To begin filtering the list in PowerShell, we will first create a variable by writing
$recyclebin = Get-PnPRecycleBinItem -RowLimit 1000
Now we have created a variable called $recyclebin which now have the list of items - you can test it, by just writing $recyclebin in your console
$recyclebin
Filter
Our next command is going to filter our variable ($recyclebin)
$recyclebin | Where-Object {$_.ItemType -eq "ListItem"}
This command is taking the variable $recyclebin and asking it to return all objects where ItemType equals ListItem
We can shorten the list even further by adding more filters
$recyclebin | Where-Object {$_.ItemType -eq "ListItem" -and $_.DirName -eq "sites/Blog/Lists/Settings"}
There are many examples of how you can filter your list, and you can find a lot more here
In my example here I am down from 200 elements, to only 2
Restoring your item
After you have located the deleted item you want to restore, we can restore it by using another PnP PowerShell command
We are going to use Restore-PnPRecycleBinItem which is documented here
One of the objects that is returned is a unique Id, so we can restore a specific item by writing
Restore-PnPRecycleBinItem -Identity 3361f787-99e7-4c06-9555-4f15f73c64bc
We will confirm by writing Y
If I run the Get-PnPRecycleBinItem command again, and filter my items, I will only have one item left, since the second item has been restored
Now you know how to find and restore items that have been deleted by accident - are you ready to become a hero among your colleagues? ;)
PS. This is a very good example of why the Title column is a good idea to use when you are designing your site
Happy restoring