Tuesday, February 18, 2020

How to search for Recycle Bin items in SharePoint online

Introduction: 

At times you may need to search for the recycle bin items in SharePoint online. Unfortunately there is no easy way to find the items in GUI, going to Site Contents --> Recycle Bin. However there is a way to export the recycle bin items to CSV using powershell. Good news is you do not need to have SPO admin or global admin rights to get this. Just a Site Collection Admin rights is required. A site collection admin can run this report. Let's get started.


Pre-requisites:  


  • At first you need to have PnP powershell command lets installed in your latest Powershell module. If you need the steps setting up Pnp, please refer to the link here from MSFT. 
  • You need to have Site Collection Admin to run this script. 

Steps: 

Please follow the below steps to get the report of recycle bin items based on parameters specified. 

Step1: Define the fields and initialize them.

Here i have created only site url and the user name where the users deleted items needs to be retrieved. 
$SiteURL = "URL OF YOUR SITE" #for instance https://www.contoso.com/sites/sales
Connecting to PnP online using the below script
$UserDisplayName = "DISPLAY NAME OF USER"

Step2: Connect to the site 

Run the below command to get connection to site
Connect-PnPOnline -Url $SiteURL -UseWebLogin 
#the reason to use weblogin parameter is that as in some scenarios, the MFA could be enabled for some users which is true in my case, it uses browser authentication and then establishes connection. 

Step3: Get all items from recycle bin

Run below command to get all items from recycle bin. Here i have selected only Title, DeletedByName, DeletedDate parameters only. You could also get more. Please refer to link for more details.  

$AllRecycleBinItems = Get-PnPRecycleBinItem | Select Title, DeletedByName, DeletedDate | Format-Table



Step4: Get only the items deleted by the user

You can filter from the all records using the below line of command. 

$DeletedByUser=$AllRecycyleBinItems | Where {$_.DeletedByName -eq $UserDisplayName}


Step5: Disconnect from the PnP connection

Run the below command to diconnect from the PnP established connection. 
Disconnect-PnPOnline

Complete Script: 

Below is the complete script copied from command shell. Please note that the fields $SiteURL and $DeletedByName is just for reference. The Display Name can be easily found either from SharePoint user properties or from Local AD properties, using ADUC controls. 

Note: This script is executed on PowerShell version 5.1.17763.771 in conjunction with PnP version 3.13.1909.0


$SiteURL = "https://contoso.com/sites/sales"
$DeletedByName = "UserFirstName UserLastName"
$FilePath = "PathToReportFile"

#connecting to site
Connect-PnPOnline -Url $SiteURL -UseWebLogin

#Getting all recycle bin items and storing in variable 
$AllRecycyleBinItems=Get-PnPRecycleBinItem | Select Title, DeletedByName, DeletedDate 

#Getting recycle bin items which are deleted by a specific user
$DeletedByUser=$AllRecycyleBinItems | Where {$_.DeletedByName -eq $DeletedByName} | Export-Csv -Path $FilePath

#To disconnect from PnP online
Disconnect-PnPOnline


Hope this is helpful. 

Thanks and Regards,

Vinay A.