SharePoint Online – How to get the size of recycle bin using PnP PowerShell

Hi All,
Greetings ! LIFE IS BEAUTIFUL 🙂
Background: While reviewing the storage usage of one of our SharePoint sites, I noticed that we were receiving storage warning messages. There was also a notification recommending that the recycle bin be emptied.
As a result, I decided to check how much content was currently stored in the recycle bin.
PowerShell come to rescue, which inspired this article.
In this article, we will discuss the steps to determine the size of the SharePoint recycle bin using PowerShell.
Prerequsites
- Site Collection Administrator role
Details
Module Check
- Check if PnP.PowerShell module is installed. If its not installed, install the module using CMDLET – Install-Module
if (Get-Module -ListAvailable -Name "PnP.PowerShell") { Write-Host "Module is installed." } else { Write-Host "Module is not installed." Install-Module PnP.PowerShell -Scope CurrentUser -Force }
- Check if Module is Imported. If its not imported, import the module
if(-not (Get-Module -Name "PnP.PowerShell")) { Import-Module -name "PnP.PowerShell" }
- Connect to the SharePoint site, for which we need determine the recycle bin content size.
- We will use the Connect-PnPOnline CMDLET to establish a connection to the respective site.
#parameter declartion
$clientID = "<my client id>"$tenantId = "knowledgejunction1.onmicrosoft.com" $certificatePath = "C:\Prasham\m365junction.pfx"$SiteURL = "https://knowledgejunction1.sharepoint.com/"$CertificatePassword = "<my certificate password>"$CertificatePassword = (ConvertTo-SecureString -AsPlainText $CertificatePassword -Force)$KJSite = Connect-PnPOnline -Url $siteURL -ClientId $ClientId -Tenant $tenant -CertificatePath $CertificatePath -CertificatePassword $CertificatePassword -ReturnConnectionif($KJSite){ Write-Host Connected to the KJ tenant successfully }
- Once we successfully connect to the site, the next step is to retrieve the recycle bin items and calculate their total size.
#get first stage recycle bin items size
$FirstStageRecycleBinItems = Get-PnPRecycleBinItem -Connection $KJSite
#ensure, first stage recycle bin is not empty
$FirstStageRBItemsCount = $RecycleBinItems.Count
Write-Host "Total number of items in First Stage Recycle Bin " $FirstStageRBItemsCount
if($FirstStageRBItemsCount -gt 0)
{
$FirstStageRBSize = ($FirstStageRecycleBinItems | Measure-Object -Property Size -Sum).Sum
Write-Host "First Stage Recycle Bin Item Size in " $FirstStageRBSize
$FirstStageRBSize_MB = [Math]::Round($FirstStageRBSize / 1MB, 2)
Write-Host "First Stage Recycle Bin Item Size in MB " $FirstStageRBSize_MB
}
#get second stage recycle bin items size
#calculating size of Second Stage recycle bin items $SecondStageRecycleBinItems = Get-PnPRecycleBinItem -Connection $KJSite -SecondStage #ensure, second stage recycle bin is not empty $SecondStageRBItemsCount = $SecondStageRecycleBinItems.Count Write-Host "Total number of items in Second Stage Recycle Bin " $SecondStageRBItemsCount if($SecondStageRBItemsCount -gt 0) { $SecondStageRBSize = ($SecondStageRecycleBinItems | Measure-Object -Property Size -Sum).Sum Write-Host "Second Stage Recycle Bin Item Size in " $SecondStageRBSize $SecondStageRBSize_MB = [Math]::Round($SecondStageRBSize / 1MB, 2) Write-Host "Second Stage Recycle Bin Item Size in MB " $SecondStageRBSize_MB }
Complete SCRIPT
<#.SYNOPSIS This script calculates the size of first stage and second stage recycle bin of given URL .DESCRIPTION PowerShell script to get the size of first stage and second stage recycle bin.NOTES Author: Prasham Sabadra Version 1.0 - initial release#># High level steps# 1. Connect to the site for which we need to get the Reccycle Bin items size# 2. Get the Recycle Bin items# 3. Get the size of first level recycle bin# 4. Get the size of second level recycle bin#region - Parameter declaration $tenant = "knowledgejunction1.onmicrosoft.com" $siteURL = "https://knowledgejunction1.sharepoint.com/" $ClientId = "<my client id>" $CertificatePath = "C:\Users\u1086350\Documents\Prasham\m365KJ.pfx" $CertificatePassword = "<my certificate password>"#endregion#region - Module Check # Check if PnP PowerShell is installed if (Get-Module -ListAvailable -Name "PnP.PowerShell") { Write-Host "Module is installed." } else { Write-Host "Module is not installed." Install-Module PnP.PowerShell -Scope CurrentUser -Force } #As we are using PnP PowerShell module, check if PnP PowerShell module is imported if(-not (Get-Module -Name "PnP.PowerShell")) { Import-Module -name "PnP.PowerShell" }#endregion#region - Connect to the site for which we need to get the Reccycle Bin items size $CertificatePassword = (ConvertTo-SecureString -AsPlainText $CertificatePassword -Force) $KJSite = Connect-PnPOnline -Url $siteURL -ClientId $ClientId -Tenant $tenant -CertificatePath $CertificatePath -CertificatePassword $CertificatePassword -ReturnConnection if($KJSite) { Write-Host Connected to the KJ tenant successfully }#endregiontry{#region - Read the recycle bin values #calculating size of First Stage recycle bin items #get the recycle bit items - by default it returns items from the first stage recycle bin $FirstStageRecycleBinItems = Get-PnPRecycleBinItem -Connection $KJSite #ensure, first stage recycle bin is not empty $FirstStageRBItemsCount = $RecycleBinItems.Count Write-Host "Total number of items in First Stage Recycle Bin " $FirstStageRBItemsCount if($FirstStageRBItemsCount -gt 0) { $FirstStageRBSize = ($FirstStageRecycleBinItems | Measure-Object -Property Size -Sum).Sum Write-Host "First Stage Recycle Bin Item Size in " $FirstStageRBSize $FirstStageRBSize_MB = [Math]::Round($FirstStageRBSize / 1MB, 2) Write-Host "First Stage Recycle Bin Item Size in MB " $FirstStageRBSize_MB } #calculating size of Second Stage recycle bin items $SecondStageRecycleBinItems = Get-PnPRecycleBinItem -Connection $KJSite -SecondStage #ensure, second stage recycle bin is not empty $SecondStageRBItemsCount = $SecondStageRecycleBinItems.Count Write-Host "Total number of items in Second Stage Recycle Bin " $SecondStageRBItemsCount if($SecondStageRBItemsCount -gt 0) { $SecondStageRBSize = ($SecondStageRecycleBinItems | Measure-Object -Property Size -Sum).Sum Write-Host "Second Stage Recycle Bin Item Size in " $SecondStageRBSize $SecondStageRBSize_MB = [Math]::Round($SecondStageRBSize / 1MB, 2) Write-Host "Second Stage Recycle Bin Item Size in MB " $SecondStageRBSize_MB }#endregion} #trycatch{ <# Logging the error details occurred in error file #> Write-Host $($_.Exception.Message) Add-Content ErrorLogs.txt $($_.Exception.Message)}#catchfinally { <#Do this after the try block regardless of whether an exception occurred or not#>}
References
- Get-PnPRecycleBinItem – https://pnp.github.io/powershell/cmdlets/Get-PnPRecycleBinItem.html
- PowerShell Modules / CMDLETs – Connecting to Microsoft 365 Services – https://knowledge-junction.in/2025/06/09/powershell-connecting-to-microsoft-365-services/
- PowerShell : Getting Started with PnP PowerShell Cmdlets – https://knowledge-junction.in/2025/06/04/powershell-pnp-powershell/
- PowerShell tutorial : Chapter 1 – Beginning with PowerShell – https://knowledge-junction.in/2023/08/20/powershell-tutorial-beginning-with-powershell/
Thank you for reading. HAVE a WONDERFUL TIME 🙂
