PowerShell: Report on size of first and second stage recycle bin for SP Online

Copper Contributor

Wondering if anyone can share a powershell script to query across either the first and second stage recycle bin (or ideally both) for all site collections in a SharePoint Online tenant?

 

Thanks in advance

3 Replies
I wrote two scripts about accessing SPO Recycle bin that could help you:
- Working with Site Collection Reycle Bin: https://gallery.technet.microsoft.com/How-to-get-recycle-bin-409cadb5
- Working with Site Recycle Bin: https://gallery.technet.microsoft.com/How-to-get-recycle-bin-4f581908

You can use this script to get report on size of first and second stage recycle bin for all site collections. It does not include hidden site collections (OneDrive, Office365 Groups).

 

function Get-sizeSiteCollections 
{ 
 param ($SiteUrl,$UserName,$Password) 

$loadInfo1 = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
$loadInfo2 = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")
$loadInfo3 = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.UserProfiles")
#$sstr = ConvertTo-SecureString -string $AdminPass -AsPlainText -Force
$Securepass = ConvertTo-SecureString $Password -AsPlainText -Force
$context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)

$context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,$Securepass)
$site = $context.Site
$recycleBinCollection = $site.RecycleBin
$context.Load($site)
$context.Load($recycleBinCollection)
$context.ExecuteQuery()
$FSFiles=@()
$SSFiles=@()

foreach($item in $recycleBinCollection){
        if($item.itemState -eq "FirstStageRecycleBin"){
          
          if($item.Size)
          {
          $FSSize+=$item.Size
          }
            
        }
        else 
        {
        if($item.Size)
          {
          $SSSize+=$item.Size
          }
        }
}
If($FSSize -eq $null)
{
$FSSize ="Empty"
}
Else
{
$FSSize=$FSSize/1MB
}
If($SSSize -eq $null)
{
$SSSize ="Empty"
}
Else
{
$SSSize=$SSSize/1MB
}

Write-Host "Recyle bin size  Of:"$SiteUrl -ForegroundColor Yellow
Write-Host "---------------------" -ForegroundColor Red
Write-Host "First Stage Recyle bin Total size(MB)  Of ="$FSSize   -ForegroundColor Cyan
Write-Host "Second Stage Recyle bin Total size(MB)  Of ="$SSSize -ForegroundColor Cyan
Write-Host "---------------------" -ForegroundColor Green
$FSSize=$null
$SSSize=$null
  }

  function Get-SPOALLSiteCollections 
{ 
    param ($SiteUrl,$UserName,$Password) 
    
                
      $loadInfo1 = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
      $loadInfo2 = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")
      Add-Type -Path "C:\Program Files\SharePoint Client Components\16.0\Assemblies\Microsoft.Online.SharePoint.Client.Tenant.dll" 

        $Securepass = ConvertTo-SecureString $Password -AsPlainText -Force
        $context = New-Object Microsoft.SharePoint.Client.ClientContext($sSiteUrl)  
        $context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $Securepass)   
        $Tenant= New-Object Microsoft.Online.SharePoint.TenantAdministration.Tenant($context) 
        $SiteCollections=$Tenant.GetSiteProperties(0,$true) 
        $context.Load($SiteCollections) 
        $context.ExecuteQuery() 
         
       foreach($SiteCollection in $SiteCollections)
        {             
         Get-sizeSiteCollections  -SiteUrl $SiteCollection.Url -UserName $UserName -Password $Password
        } 
       
} 
 
#Required Parameters 
$sSiteUrl = "https://<tenantname>-admin.sharepoint.com/"  
$UserName = "admin@<tenantname>.onmicrosoft.com"  
$Password ="password"

Get-SPOALLSiteCollections -SiteUrl $sSiteUrl -UserName $UserName -Password $Password 
How can the script be modified to see the size of our Teams/Office 365 groups channels recycle bins?