SharePoint Online: View Folder Size using PowerShell

Requirement: Get a Folder Size in SharePoint Online.

How to Check a Folder Size in SharePoint Online?

Managing SharePoint content can be a daunting task, especially when you have to find out what size your folders are. You will need to know the total size of your folder in order to determine how much space is left on your site and for future data storage allocation. In this blog post, I’ll show you how to check the size of a folder in SharePoint Online.

How to check folder size in SharePoint Online? To view folder size in SharePoint Online, go to:

  1. Navigate to the site >> Click on Settings gear >> Site Settings >> Click on “Storage Metrics” link under “Site Collection Administration”.
  2. On the storage metrics page, click on the document library to navigate to the location where the target folder is stored.
    sharepoint online get folder size

The storage metrics page shows the size of each folder and file (including version history size). It is worth mentioning that the storage metrics page data is not real-time data! It takes a while for a background timer job to run and populate this data.

SharePoint Online: Get Folder Size using PowerShell

How to find folder size in SharePoint Online? Well, let’s check folder size for all folders in a SharePoint Online site using the PowerShell script:

#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)
        #Get all Files and Subfolders from the folder

        $FolderSize = 0
        ForEach($File in $Folder.Files | Where {-Not($_.Name.EndsWith(".aspx"))})
            #Get File versions
            If($File.Versions.Count -ge 1)
                #Calculate Version Size
                $VersionSize = $File.Versions | Measure-Object -Property Size -Sum | Select-Object -expand Sum                                
            $FileSize =  [Math]::Round((($File.Length) + $VersionSize)/1KB, 2)
            If($FileSize -gt 0)
                Write-host "`tSize of the File '$($File.Name)' "$FileSize

            #Get File Size
            $FolderSize += $FileSize

        If($FolderSize -gt 0)
            Write-host -f Yellow "Total Size of the Folder '$($Folder.ServerRelativeUrl)' (KB): " -NoNewline
            $FolderSize= [Math]::Round($FolderSize/1KB, 2)
            Write-host $FolderSize
        #Process all Sub Folders
        ForEach($Folder in $Folder.Folders | Where {-Not($_.Name.StartsWith("_") -or $_.Name -eq "Forms")})
            #Call the function recursively
            Get-SPOFolderSize $Folder
    Catch [System.Exception]
        Write-Host -f Red "Error:"$_.Exception.Message
$SiteURL = ""

#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
#Call the function to get Subsite size
Get-SPOFolderSize -Folder $RootFolder

How to view folder size in SharePoint Online using PnP PowerShell?

Instead of summarizing the size of each file and its versions, we can directly get the size of the folder (Files + Versions) from its property: SMTotalSize!

$SiteURL = "" 
$FolderSiteRelativeURL = "/Branding/Active"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
#Get the folder
$Folder = Get-PnPFolder -Url $FolderSiteRelativeURL -Includes ListItemAllFields

#Get the total Size of the folder - with versions
Write-host "Size of the Folder:" $([Math]::Round(($Folder.ListItemAllFields.FieldValues.SMTotalSize.LookupId/1KB),2))

How about finding the sizes of all folders in a document library?

$SiteURL = "" 
$ListName = "Branding"
$CSVFile = "C:\Temp\FolderSize.csv"

Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Interactive
    #Get all folders from the document library
    $Folders = Get-PnPListItem -List $ListName -PageSize 2000 | Where { $_.FileSystemObjectType -eq "Folder" }
    #Calculate Folder Size from files
    $FolderSizeData = @()
    $Folders | ForEach-Object {
        #Extract Folder Size data
        $FolderSizeData += New-Object PSObject -Property  ([Ordered]@{
            "Folder Name"  = $_.FieldValues.FileLeafRef
            "URL" = $_.FieldValues.FileRef        
            "Size" = $_.FieldValues.SMTotalSize.LookupId
    $FolderSizeData | Format-Table
    $FolderSizeData | Export-csv $CSVFile -NoTypeInformation
    #Calculate the Total Size of Folders
    $FolderSize = [Math]::Round((($FolderSizeData | Measure-Object -Property "Size" -Sum | Select-Object -expand Sum)/1KB),2)
    Write-host -f Green ("Total Size: {0}" -f $FolderSize)
Catch {
    Write-Host -f Red "Error:"$_.Exception.Message

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

Leave a Reply

Your email address will not be published. Required fields are marked *