SharePoint Online: Get Subsite Size using PowerShell

Requirement: Get Subsite Size in SharePoint Online

How to Get the Subsite Size in SharePoint Online?

Are you looking for a way to get the size of your subsites in SharePoint Online? Well, In this post, I will show you how to get subsite size information in SharePoint Online. Also, I will share the script to find the subsite size in SharePoint Online using PowerShell.

To find out the storage quota occupied by a subsite in SharePoint Online, go to:

  • Site Settings >> Click on “Storage Metrics” link under “Site Collection Administration”
    sharepoint online subsite size

This page gets you the size of each subsite from the particular site.

Please note, the data shown in storage metrics is not real time! It takes a while for a background timer job to run and populate this data.

Get Subsite Size in SharePoint Online using PowerShell

Here is the PowerShell to get subsite size in SharePoint Online:

#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"
 
#Function to Get the size of a Folder in SharePoint Online
Function Get-SPOFolderSize([Microsoft.SharePoint.Client.Folder]$Folder)
{ 
     Try
     {
        Write-host -f Yellow "Size of the Folder '$($Folder.ServerRelativeUrl)' : " -NoNewline
        
        $FolderSize = 0
        #Get all Files and Subfolders from the folder
        $Ctx.Load($Folder.Files)
        $Ctx.Load($Folder.Folders)
        $Ctx.ExecuteQuery()

        ForEach($File in $Folder.Files)
        {
            #Get File Size
            $FolderSize += $File.Length
        }
        Write-host $FolderSize

        #Process all Sub Folders
        ForEach($Folder in $Folder.Folders)
        {
            #Call the function recursively
            $FolderSize +=Get-SPOFolderSize $Folder
        }
        Return $FolderSize
    }
    Catch [System.Exception]
    {
        Write-Host -f Red "Error:"$_.Exception.Message
    }
}
 
#parameters
$SiteURL = "https://Crescent.sharepoint.com/unitedstates"
 
#Get credentials to connect to SharePoint Online Admin Center
$Cred = Get-Credential
 
#Set up the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
   
#Get the Web
$Web = $Ctx.Web
$RootFolder = $Web.RootFolder
$Ctx.Load($RootFolder)
$Ctx.ExecuteQuery()

#Call the function to get Subsite size
$SubSiteSize = Get-SPOFolderSize -Folder $RootFolder

#sharepoint online powershell site size
Write-host "Toal Size of the subsite (MB): " -NoNewline
[Math]::Round($SubSiteSize/1MB, 2)

This PowerShell script gets the size of a given subsite. How about retrieving storage consumption of all subsites in a site collection?

PowerShell to Find Size of All Subsites in a SharePoint Online Site Collection

Let’s get the size of all subsites in a site collection and export them to a CSV file.

#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"
 
#Function to Get the size of a Folder in SharePoint Online
Function Get-SPOFolderSize([Microsoft.SharePoint.Client.Folder]$Folder)
{ 
     Try
     {
        Write-host -f Cyan "`t Size of the Folder '$($Folder.ServerRelativeUrl)' : " -NoNewline
        
        $FolderSize = 0
        #Get all Files and Subfolders from the folder
        $Ctx.Load($Folder.Files)
        $Ctx.Load($Folder.Folders)
        $Ctx.ExecuteQuery()

        ForEach($File in $Folder.Files)
        {
            #Get File Size
            $FolderSize += $File.Length
        }
        Write-host $FolderSize

        #Process all Sub Folders
        Foreach($Folder in $Folder.Folders)
        {
            #Call the function recursively
            $FolderSize +=Get-SPOFolderSize $Folder
        }
        Return $FolderSize
    }
    Catch [System.Exception]
    {
        Write-Host -f Red "Error:"$_.Exception.Message
    }
}

Function Get-SPOSubsiteSize([Microsoft.SharePoint.Client.Web]$Web)
{  
    Write-host -f Yellow "Finding the Size of the subsite:"$web.Url 
    $StorageMetrics= @()

    #Get the Root Folder of the Web
    $Ctx = $Web.Context
    $RootFolder = $Web.RootFolder
    $Ctx.Load($RootFolder)
    $Ctx.Load($Web.Webs)
    $Ctx.ExecuteQuery()

    #Call the function to get Subsite size
    $SubSiteSize = Get-SPOFolderSize -Folder $RootFolder
    $SizeinMB = [Math]::Round($SubSiteSize/1MB, 2)
    Write-host -f Green "Toal Size of the subsite (MB): $SizeinMB `n"
    
    #Add the result to an Array
    $StorageData = New-Object PSObject
    $StorageData | Add-Member NoteProperty SiteURL($web.Url)
    $StorageData | Add-Member NoteProperty Size-MB([math]::Round($SubSiteSize/1MB,2))
    $StorageMetrics += $StorageData

    #Process all subsites
    ForEach($Subweb in $web.Webs)
    {         
        $StorageMetrics+=Get-SPOSubsiteSize $Subweb
    }
    Return $StorageMetrics
}

#parameters
$SiteURL = "https://Crescent.sharepoint.com"
$CSVPath="C:\Temp\SubsiteStorage.csv"

#Delete the Output Report, if exists
If (Test-Path $CSVPath) { Remove-Item $CSVPath }

#Get credentials to connect to SharePoint Online Admin Center
$Cred = Get-Credential
 
#Set up the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
   
#Get the Root Web
$Web = $Ctx.Web
$Ctx.Load($Web)
$Ctx.ExecuteQuery()

#Call the function to get subsite storage metrics
$SubsiteStorageMetrics = Get-SPOSubsiteSize $Web

#Export the results to CSV
$SubsiteStorageMetrics | Export-CSV -LiteralPath $CSVPath -NoTypeInformation

If you want to get the site collection size in SharePoint Online using PowerShell, use: Check SharePoint Online Site Collection Storage Size using PowerShell

Salaudeen Rajack

Salaudeen Rajack is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

2 thoughts on “SharePoint Online: Get Subsite Size using PowerShell

  • Hi, How can you get the count of subsites in a site collection?

    Reply
    • Use this script:
      #Connect to PnP Online
      Connect-PnPOnline -Url “https://crescent.sharepoint.com/sites/marketing” -Interactive

      #Get Subsites count
      (Get-PnPSubWeb -Recurse).count

      Reply

Leave a Reply