Learn how to retrieve deleted items with PnP PowerShell, filter the recycle bin, and restore them


The challenge

There is a recycle bin available for your site, but the filter / search options are limited.

Solution

We can use PowerShell 7 and the PnP PowerShell scripts to collect the deleted items, and thereafter filter the list

Connect to your site

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

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

Как восстановить удаленные группы office 365 с использованием power shell

Filtering your list

  • Title
  • ItemType
  • LeafName
  • DirName

To begin filtering the list in PowerShell, we will first create a variable by writing

 

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

Filter
Our next command is going to filter our variable ($recyclebin)

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

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

Как восстановить удаленные группы office 365 с использованием power shell

Restoring your item

After you have located the deleted item you want to restore, we can restore it by using another PnP PowerShell command

:/>  Как стереть все сообщения google и как удалить все письма в gmail сразу или от одного получателя

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

 

Как восстановить удаленные группы office 365 с использованием power shell

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

Как восстановить удаленные группы office 365 с использованием power shell

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


See also