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 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 from the site settings page and with PowerShell script.

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 the “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

As the amount of data stored in SharePoint libraries grow, it is important to monitor and manage the size of the folders and libraries. One way to do this is by using PowerShell, which provides a powerful scripting environment to automate tasks and retrieve information about SharePoint Online.

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)
{ 
     Try
     {
        #Get all Files and Subfolders from the folder
        $Ctx.Load($Folder.Files)
        $Ctx.Load($Folder.Folders)
        $Ctx.ExecuteQuery()

        $FolderSize = 0
        ForEach($File in $Folder.Files | Where {-Not($_.Name.EndsWith(".aspx"))})
        {
            #Get File versions
            $Ctx.Load($File.Versions)
            $Ctx.ExecuteQuery()
            $VersionSize=0
            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
    }
}
  
#parameters
$SiteURL = "https://Crescent.sharepoint.com"

#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
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 folder size (Files + Versions) from its property: SMTotalSize!

#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/marketing" 
$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?

#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/marketing" 
$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
}

Conclusion:

In this tutorial, we discussed how to retrieve the size of a folder in SharePoint Online using the storage metrics page and PowerShell. By using the SharePoint Client Side Object Model (CSOM) and a PnP PowerShell script, we were able to retrieve the size of a folder and its subfolders, providing valuable information for managing the storage usage of a SharePoint Online environment. This method can be used as part of a larger automation process, helping to keep your SharePoint environment organized and optimized.

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 *