How to Get SharePoint Document Library Size using PnP PowerShell?

One of my clients recently asked how to check the size of a specific SharePoint document library. The easiest way is through SharePoint’s Storage Metrics, which is available in the site settings. It provides the total size, %of Site Quota, etc.

In this article, I will also explain how to get the SharePoint document library size using PnP PowerShell.

Get SharePoint Library Size From SharePoint Site Settings

In the image below, you can see, under Storage Metrics, the total size and percentage of parent, as well as the percentage of Site Quota, for all system libraries and custom libraries.

get sharepoint document library size powershell

Follow the steps below to get the size of a particular SharePoint document library.

1. In the SharePoint site, open the Site Contents -> Click on Site settings.

Get Document Library Size using PowerShell

2. In Site Settings ->Under Site Collection Administration -> Open Storage Metrics.

How to get size of document library in SharePoint Online

Now, as in the intro image, it will display the storage of each library from SharePoint.

Get SharePoint Library Size Using PnP PowerShell

Let’s see how to use PnP PowerShell to retrieve the size of a SharePoint document library.

In the image below, you can see in the Terminal pane of VS Code that the size [3.65 MB] of the provided document library [Employee Travel Authorization PDF Files] is displayed.

get sharepoint library size using pnp powershell

Let’s understand the Pnp PowerShell code that returns the size of the particular SharePoint document library.

$SiteURL = "https://<tenant name>.sharepoint.com/sites/TravelAuthorization"
$LibraryName = "Employee Travel Authorization PDF Files"
$ClientID = "your clinet id"

Connect-PnPOnline -Url $SiteURL -ClientId $ClientID -Interactive

# Get the storage metric of the library
$LibraryStorage = Get-PnPFolderStorageMetric -List $LibraryName

# Show TotalSize in MB
$LibrarySizeMB = [Math]::Round($LibraryStorage.TotalSize / 1MB, 2)
Write-Host "Total Library Size: $LibrarySizeMB MB"

Here,

  • $SiteURL = Provide your site url.
  • $LibraryName = Provide library name.
  • $ClientID = Provide the client ID.
  • Get-PnPFolderStorageMetric = Allows retrieval of storage metrics for a folder in SharePoint Online, which is stored in the $LibraryStorage variable.
  • [Math]:: Round($LibraryMetric.TotalSize / 1MB, 2) = It takes the total size in bytes, converts it into megabytes, and rounds it to two decimal places.

Get All SharePoint Libraries Sizes Using PnP PowerShell

In this section, we will learn how to retrieve the size of all libraries present on the SharePoint site using PnP PowerShell.

In the image below, you can observe that the TERMINAL pane returns the library names along with their sizes.

how to get all sharepoint document libraries size powershell
$SiteURL = "https://<tenant name>.sharepoint.com/sites/SalesDepartmentTeam"
$ClientID = "your client ID"
Connect-PnPOnline -Url $SiteURL -ClientId $ClientID -Interactive

# Get all document libraries (BaseTemplate 101 = Document Library)
$SP_Doc_Libraries = Get-PnPList | Where-Object { $_.BaseTemplate -eq 101}

# Array to store the results
$Storage_Results = @()

foreach ($Library in $SP_Doc_Libraries) {
    try {
        # Get storage details of the libraries
        $Storagedata = Get-PnPFolderStorageMetric -List $Library.Title
        $SizeMB = [Math]::Round($Storagedata.TotalSize / 1MB, 2)
        $Storage_Results += [PSCustomObject]@{
            "Library Name" = $Library.Title
            "Size (MB)"    = $SizeMB
        }
    }
    catch {
        Write-Warning "Couldn't get size for library: $($Library.Title)"
    }
}
$Storage_Results | Sort-Object "Size (MB)" -Descending | Format-Table -AutoSize

Here,

  • $SP_Doc_Libraries = Stores all library names from the given SharePoint site. To retrieve only libraries, we use the Where object to apply a filter that BaseTemplate -eq 101, where 101 indicates a library.
  • $Storage_Results = @() Craeting an empty array.
  • Within the foreach loop under the try block, we use the Get-PnPFolderStorageMetrics command to retrieve the storage information for all the libraries.
  • [Math]:: Round($Metric.TotalSize / 1MB, 2) = Convert bytes to MB and round to 2 decimals.
  • PSCustomObject = Creating one custom object with the properties like
    • Library Name
    • Size(MB)
  • That object is stored in the Results array.
  • To handle the errors, we’re using a catch block.
  • $Storage_Results | Sort-Object “Size (MB)” -Descending | Format-Table -AutoSize = It returns the results in a table format where the size is in descending order.

This way, we can get the SharePoint document library size using PnP PowerShell.

I hope you found this article helpful!

In this article, I explain how to get the size of a SharePoint document library from site settings, as well as using PnP PowerShell. Additionally, I explained how to retrieve the sizes of all SharePoint libraries using PnP PowerShell.

Also, you make like!

Power Apps functions free pdf

30 Power Apps Functions

This free guide walks you through the 30 most-used Power Apps functions with real business examples, exact syntax, and results you can see.

Download User registration canvas app

DOWNLOAD USER REGISTRATION POWER APPS CANVAS APP

Download a fully functional Power Apps Canvas App (with Power Automate): User Registration App