Get SharePoint Library Size with PowerShell

SharePoint 2010 document library sizes can be easily retrieved via Storage metrics: Site Actions >> Site Settings >> Storage Metrics if you have SP1 installed.

sharepoint get document library size

You can get SharePoint library size, ONLY when you have Quotas applied to the site collection. Otherwise, you get “The storage space allocation page cannot be used for sites that do not have a storage quota defined.” error message in MOSS 2007.

The storage space allocation page cannot be used for sites that do not have a storage quota defined

How to find a document library’s size in SharePoint programmatically?

Let’s see the PowerShell approaches to find SharePoint 2007/SharePoint document library size, picture library size, or for any other
libraries.

Approach 1: Using StorageManagementInformation API:
It retrieves storage management information about the site collection. More info on StorageManagementInformation , It takes these parameters:

1. ltVar: Which object
        List = 1
        DocumentLibrary = 2
        Document = 3
2. sordVar: sort order
        Increasing = 0×10
        Decreasing = 0×11
3. soVar: Sort based on size or date
        Size=0
        Date = 1
4. nMaxResults: number of results to return

Lets get the size of the “Shared Documents” Library with PowerShell:

#SharePoint library size report
#Get the Site collection
$Site = Get-SPsite "https://sharepoint.crescent.com"

#Returns a DataTable similar to "Storage Management Page" in Site settings
$DataTable = $Site.StorageManagementInformation(2,0x11,0,0)

#Loop through the Rows and Fetch the row matching "Shared Documents" in subsite "team"
foreach($Row in $DataTable.Rows)
{
    if ($Row.Title -eq "Shared Documents" -and $Row.Directory -eq "team")
        {
            $LibrarySize = [Math]::Round(($Row.Size/1MB),2)
            Write-Host $LibrarySize "MB"
        }
}

You can retrieve Top 10 large libraries based on their size as:

#Get the Site collection
$Site = Get-SPsite "https://sharepoint.crescent.com"

#Returns a DataTable similar to "Storage Management Page" in Site settings
$DataTable = $Site.StorageManagementInformation(2,0x11,0,10)

$DataTable | Select Title, ItemCount, Size, Directory | Format-Table

and the output:

However MSDN says this API is obsolete. So lets try with an alternate approach. Found this Article in MSDN on getting the storage information through object model.

Approach 2: Iterate through each document and its versions stored in the library to calculate the library size:
Lets use PowerShell to iterate each folder & sub-folder of a particular document library. This script can be used in MOSS 2007 or SharePoint 2010 get library size.

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

# Function to calculate Library size
Function GetLibrarySize($Folder)
{
    $FolderSize = 0
    foreach ($File in $Folder.Files)
    {
        #Get File Size
        $FolderSize += $File.TotalLength;

        #Get the Versions Size
        foreach ($FileVersion in $File.Versions)
        {
            $FolderSize += $FileVersion.Size
        }
    }

      #Get Files in Sub Folders
        foreach ($SubFolder in $Folder.SubFolders)
        {
           if($SubFolder.Name -ne "Forms") #Leave "Forms" Folder which has List default Aspx Pages.
             {
                 $FolderSize += GetLibrarySize($SubFolder)
             }
        }

       return [Math]::Round(($FolderSize/1MB),2)
}

$Web = Get-SPWeb "https://sharepoint.crescent.com/team/"

#Get the Library's Root Folder
$Library =  $Web.Lists["Shared Documents"].RootFolder

#Call the function to Calculate Size
$LibrarySize=GetLibrarySize($Library)

Write-Host "Library Size:" $LibrarySize "MB"

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

3 thoughts on “Get SharePoint Library Size with PowerShell

  • Hello,

    Could you please help me in modifying the script to get the library/list size from entire farm and export to excel.

    Thanks in Advance.

    Reply
  • Does this work on WSS 3?? I can get the first script to work fine but the 2nd just errors:

    The string starting:
    At C:get-versionedlibraries.ps1:68 char:41
    + if($list.BaseType -eq “DocumentLibrary <<<< ") is missing the terminator: ". At C:get-versionedlibraries.ps1:101 char:1 + <<<< + CategoryInfo : ParserError: () { ...Csv $filePath :String) [], ParseException + FullyQualifiedErrorId : TerminatorExpectedAtEndOfString

    Reply
    • Hi Rick,

      This code is written for SharePoint 2010 and would cause error when you try to run it in MOSS 2007 environment. Say for e.g cmdlet Get-SPSite doesn’t exists for MOSS 2007; There is no PowerShell Snap-in available for MOSS 2007, isn’t it?

      However, You can make the code work in MOSS 2007 with very few changes! Refer my other posts :
      SharePoint SubSite Size Storage Report

      How to use PowerShell in MOSS 2007

      Let me know if you have any further questions.

      Regards,
      Salaudeen Rajack

      Reply

Leave a Reply

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