SharePoint Online: Get Recycle Bin Storage Size using PowerShell

Requirement: Get the recycle bin storage space consumption of all SharePoint Online site Collections.
Get Recycle Bin Size in SharePoint Online using PowerShell

SharePoint Online: Find Recycle Bin Storage Size of a Site Collection
#Load SharePoint CSOM 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://crescenttech.sharepoint.com/Sites/Marketing"

#Setup Credentials to connect
$Cred = Get-Credential
  
Try {
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
  
    #Get the Site collection's recycle bin: both 1st stage and 2nd stage
    $RecycleBinItems = $Ctx.Site.RecycleBin
    $Ctx.Load($RecycleBinItems)
    $Ctx.ExecuteQuery()

    #Get Size of the Recycle bin items
    $RecycleBinSize = $RecycleBinItems | Measure-Object Size -Sum

    Write-Host -f Green "Total Recycle Bin Size: $($RecycleBinSize.Sum/1MB) MB"
    
}
Catch {
    write-host -f Red "Error:" $_.Exception.Message
}

PowerShell to Get SharePoint Online Recycle bin Storage Size:
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.Online.SharePoint.Client.Tenant.dll"
 
#Function to Get the recycle bin Size of a SharePoint Online site collection
Function Get-SPOSiteRecycleBinSize($SiteURL, $Cred)
{ 
    #Setup credentials
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

    #Set up the context
    $Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL) 
    $Context.Credentials = $Credentials
   
    $Site = $context.Site
    $RecycleBin = $Site.RecycleBin
    $context.Load($Site)
    $context.Load($RecycleBin)
    $context.ExecuteQuery()

    $FirstStageSize=0
    $SecondStageSize=0

    Foreach($Item in $RecycleBin)
    {
        If($Item.itemState -eq "FirstStageRecycleBin")
        {
          $FirstStageSize+=$Item.Size
        }
        Else
        {
          $SecondStageSize+=$Item.Size
        }
    }
    #Output the results
    $Data ="Site Collection URL {0}. First Stage Recycle Bin Size {1}. Second Stage Recycle Bin Size {2}" -f $SiteURL,$FirstStageSize,$SecondStageSize
    Write-host -f Green $Data
}

#Get All Site collections from the Tenant- Including Modern Team sites and communication sites
Function Get-SPOSites($AdminSiteURL, $Cred)
{
    #Setup credentials to connect
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
  
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($AdminSiteURL)
    $Ctx.Credentials = $Credentials
 
    #Get the tenant object 
    $Tenant = New-Object Microsoft.Online.SharePoint.TenantAdministration.Tenant($ctx)
 
    #Get All Site Collections
    $SiteCollections=$Tenant.GetSitePropertiesFromSharePoint(0,$true)
    $Ctx.Load($SiteCollections)
    $Ctx.ExecuteQuery()

    #Iterate through Each site collection
    ForEach($Site in $SiteCollections)
    {
        Write-host -f Yellow "Searching Site Collection:"$Site.URL
        Get-SPOSiteRecycleBinSize -SiteUrl $Site.Url -Cred $Cred
    }
}
 
#Set Parameters
$AdminSiteUrl = "https://crescent-admin.sharepoint.com/"
$Cred= Get-Credential

Get-SPOSites -AdminSiteURL $AdminSiteUrl -Cred $Cred
This script gets you the storage occupied by first stage and second stage recycle bins of all site collections of the tenant.

PnP PowerShell to Get Recycle Bin Size of a SharePoint Online Site Collection:
You can use the Get-PnPRecycleBinItem cmdlet to view all the items in the site collection recycle bin
#Config Variables
$SiteURL = "https://crescenttech.sharepoint.com/sites/marketing"

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

#Sum Recycle bin Items Size
$RecycleBinSize = Get-PnPRecycleBinItem | Measure-Object -Property Size -Sum

#Get Recycle bin size
Write-host "Recycle Bin Size (MB):" ([Math]::Round($RecycleBinSize.Sum/1MB,2)) 
SharePoint Online: Get Recycle Bin Storage Size using PowerShell SharePoint Online: Get Recycle Bin Storage Size using PowerShell Reviewed by Salaudeen Rajack on December 18, 2017 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.