Determine Size of SharePoint Site Collection Recycle Bin

By James|04/13/2020|,

Curious how much data is in your SharePoint on premises site collection recycle bin?  SharePoint does not provide a way to determine this.  Fortunately, PowerShell can accomplish this with relative ease.  The following PowerShell script will process site collection first and second stage recycle bins and report back on storage space used.  Remember to modify the $SiteColl variable and point it to a valid site collection first!

# Title:   Get-RecycleBinSize.ps1
# Version: 1.0, 13 APR 2020
# Author:  James Sanders
# Purpose: Calculate and display size of site collection recycle bin (first and second stage)

# Add the PowerShell Snap-In
Add-PSSnapin Microsoft.SharePoint.PowerShell -EA SilentlyContinue

# Site Collection to query
$SiteColl = "http://mybigfatsharepointsite.com/demo"

#Get the site collection
Try {
  $site = Get-SPSite $SiteColl -ErrorAction Stop
}
Catch {
  Write-Host
  Write-Host -ForegroundColor Red "- Unable to open site collection $SiteColl"
  Write-Host
  Break
}

# FIRST STAGE RECYCLE BIN

# Create SPRecycleBinQuery object to query the first stage recycle bin
$SPRecycleBinQuery = New-Object -TypeName Microsoft.SharePoint.SPRecycleBinQuery
$SPRecycleBinQuery.ItemState = [Microsoft.SharePoint.SPRecycleBinItemState]::FirstStageRecycleBin
$SPRecycleBinQuery.RowLimit = [int]::MaxValue-1

# Get the sum of 'Size' property of all recycle bin items
$RecycleBinSize = $site.GetRecycleBinItems($SPRecycleBinQuery) | Measure-Object -Property Size -Sum | Select-Object -ExpandProperty Sum

# Calculate the value in MB
$size = ([System.Math]::Round(($RecycleBinSize/1Mb),2))
write-host "First Stage Recycle Bin Size (MB):" $size

# SECOND STAGE RECYCLE BIN

# Create SPRecycleBinQuery object to query the second stage recycle bin
$SPRecycleBinQuery=new-object Microsoft.SharePoint.SPRecycleBinQuery
$SPRecycleBinQuery.ItemState = [Microsoft.SharePoint.SPRecycleBinItemState]::SecondStageRecycleBin
$SPRecycleBinQuery.RowLimit = 2000
$size=0

DO {
  $SPRecycleBinItemCollection = $site.GetRecycleBinItems($SPRecycleBinQuery)
  $SPRecycleBinQuery.ItemCollectionPosition = $SPRecycleBinItemCollection.ItemCollectionPosition

  For($i=$SPRecycleBinItemCollection.Count-1; $i -GE 0; $i--) {
    $Size += $SPRecycleBinItemCollection[$i].Size
  }
}
While ($SPRecycleBinQuery.ItemCollectionPosition -Ne $Null)

$size = ([System.Math]::Round(($size/1Mb),2))
Write-Host "Second Stage Recycle Bin Size (MB)" $size

 

Copyright 2011 - 2024 The Lazy IT Admin | All Rights Reserved
menu-circlecross-circle linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram