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

step-by-step approach to determine the SharePoint Recycle Bin size using PnP PowerShell
step-by-step approach to determine the SharePoint Recycle Bin size 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
PowerShell
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

PowerShell
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

PowerShell
$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 -ReturnConnection
if($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

PowerShell
#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

PowerShell
<#
.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
}
#endregion
try{
#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
} #try
catch{
<#
Logging the error details occurred in error file
#>
Write-Host $($_.Exception.Message)
Add-Content ErrorLogs.txt $($_.Exception.Message)
}#catch
finally {
<#Do this after the try block regardless of whether an exception occurred or not#>
}

References

Thank you for reading. HAVE a WONDERFUL TIME 🙂

Prasham Sabadra

LIFE IS VERY BEAUTIFUL. ENJOY THE WHOLE JOURNEY :) Founder of Microsoft 365 Junction, Speaker, Author, Learner, Developer, Passionate Techie. Certified Professional Workshop Facilitator / Public Speaker. Believe in knowledge sharing. Around 20+ years of total IT experience and 17+ years of experience in SharePoint and Microsoft 365 services Please feel free me to contact for any SharePoint / Microsoft 365 queries. I am also very much interested in behavioral (life changing) sessions like motivational speeches, Success, Goal Setting, About Life, How to live Life etc. My book - Microsoft 365 Power Shell hand book for Administrators and Beginners and 100 Power Shell Interview Questions - https://www.amazon.in/Microsoft-Administrators-Beginners-Interview-Questions/dp/9394901639/ref=tmm_pap_swatch_0?_encoding=UTF8&qid=1679029081&sr=8-11

You may also like...

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Microsoft 365

Subscribe now to keep reading and get access to the full archive.

Continue reading