kwizcom banner advertisement

SharePoint Online: Get Recycle Bin Storage Size using PowerShell

Requirement: Get the recycle bin storage space consumption of all SharePoint Online site Collections.

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.
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.