SharePoint Online: Search Recycle Bin using PowerShell

Requirement: Search SharePoint online recycle bin for all files deleted by a particular user

How to search SharePoint Online Recycle Bin?

To find all items deleted by a particular user,

  • Navigate to your SharePoint Online site, Click on Site Settings Gear >> Choose “Site Settings”
  • From the Site settings page, click on “Recycle bin” from Site Collection Administration. This page gives you all deleted items in the site collection along with deleted by user data.
    sharepoint recycle bin search

However, searching SharePoint Online recycle bin through the web user interface is painful when you have a lot of deleted files. So, Let’s use PowerShell to get deleted files from Recycle bin.

PowerShell script to Search SharePoint Online Recycle bin:

Here is the SharePoint Online PowerShell to search recycle bin.

#Load SharePoint Online Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
   
##Variables for Processing
$SiteUrl = "https://crescent.sharepoint.com/sites/Sales"
$AdminUserName="[email protected]"
$DeletedByUserAccount="[email protected]"

#Get the password to connect 
$Password = Read-host -assecurestring "Enter Password for $AdminUserName"
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($AdminUserName,$Password)
 
Try {    
    #Setup the context
    $Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
    $Context.Credentials = $Credentials
    
    #Get the site recycle bin
    $Site = $Context.Site
    $RecycleBinItems = $Site.RecycleBin
    $Context.Load($Site)
    $Context.Load($RecycleBinItems)
    $Context.ExecuteQuery()

    #Get all items deleted by particular user in Recycle bin
    $DeletedByUser = $RecycleBinItems | Where {$_.DeletedByEmail -eq $DeletedByUserAccount}
    Write-Host "Total Number of Items deleted by user:" $DeletedByUser.Count
    
    #format output as table and print to console
    $DeletedByUser | Select Title, DeletedByEmail, DeletedDate | Format-Table
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

PnP PowerShell to Get SharePoint Online Recycle bin Items:

Here is how to search SharePoint Online recycle bin with PnP PowerShell.

#Config Variables
$SiteURL = "https://crescenttech.sharepoint.com/sites/marketing"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Get Recycle bin Items
Get-PnPRecycleBinItem | Select Title, ItemType, Size, ItemState, DirName, DeletedByName, DeletedDate | Format-table -AutoSize

This PowerShell script gets all items from given SharePoint Online site collection’s recycle bin:

sharepoint online powershell get recycle bin

You can export recycle bin items data to a CSV file by:

#Get Recycle bin Items and Export to CSV
Get-PnPRecycleBinItem | Select Title, ItemType, Size, ItemState, DirName, DeletedByName, DeletedDate | Export-Csv "C:\Temp\RecycleBin.csv" -NoTypeInformation

This script gets you all deleted item’s data from both first stage and second stage recycle bins.

Salaudeen Rajack

Salaudeen Rajack is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

9 thoughts on “SharePoint Online: Search Recycle Bin using PowerShell

  • June 10, 2021 at 5:34 AM

    change the connect part to this: #Connect to PnP Online Connect-PnPOnline -Url $SiteURL –UseWebLogin

    Reply
  • June 1, 2021 at 7:20 PM

    Is there a way to show the retention label?

    Reply
  • May 22, 2021 at 2:49 AM

    Nope, sorry… Can’t log into PnPOnline with MFA required this way. Any other suggestions? Thanks.

    Reply
    • August 27, 2021 at 6:49 AM

      you can use this, Connect-PnPOnline -Url -PnPO365ManagementShell -LaunchBrowser

      after you run the command it will produce a code that you can use to login your credential

      Reply
  • February 1, 2021 at 2:43 PM

    You might want to look into Restore-PnPRecycleBinItem after you have located the files from the CSV.

    Reply
  • January 15, 2021 at 6:35 PM

    Excellent post, mate!
    Is there a way to restore specific files using Powershell? I found the items I wanted but they are deep back in date so would take me forever to scroll there to restore them.

    Reply
  • September 17, 2019 at 11:47 AM

    Hi,

    Great script, but I have a question to this? Will it be possible to resore items to you local server / pc added to this script.

    Thanks.

    Reply
    • May 21, 2020 at 11:20 AM

      No! The Deleted Items can be restored only to its original location! There are no ways to directly restore a deleted item to a different location. However, You can restore and then move!

      Reply

Leave a Reply