SharePoint Online: View Folder Size using PowerShell

Requirement: Get Folder Size in SharePoint Online.

How to Check Folder Size in SharePoint Online?
To view folder size in SharePoint Online, go to:
  • Site Settings >> Click on "Storage Metrics" link under "Site Collection Administration"
  • In 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 size of each folder and files (Including version history size). Please note the data shown in storage metrics page 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 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 size of the each file and its versions, we can directly get size of the folder (Files + Versions) from its property: SMTotalSize!
$SiteURL = "" 
$FolderSiteRelativeURL =  "/Branding/Active"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -UseWebLogin
#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 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 -UseWebLogin
    #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

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.