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:
Let's check folder size for all 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

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.