Storage Analysis Report for SharePoint Document Library

Requirement: On a huge document library, We needed to analyze all documents stored and figure out what files are taking up the most space to move documents into individual libraries/folders, as this larger library causes latency issues.

PowerShell script to generate storage metrics report on SharePoint library:

Are you looking for a way to generate a storage analysis report for your SharePoint document library? If so, PowerShell may be the answer. This blog post will show you how to use PowerShell to create a storage analysis report for your SharePoint document library.

Let’s use a PowerShell script to analyze and generate a report on document library storage information:

# Function to Get Storage Metrics for a Library 
Function GetStorageMetrics($Folder)
    #Array to hold Storage data for all files
    $StorageDataCollection = @()
    foreach ($File in $Folder.Files)
        #Get File Size
        $FileSize = $File.TotalLength
        $VersionSize = 0
        #Get the Versions Size
        foreach ($FileVersion in $File.Versions)
            $VersionSize +=$FileVersion.Size
        $TotalFileSize = $FileSize + $VersionSize
        #Create an object to hold storage data
        $StorageDataResult = New-Object PSObject
        #Set storage details to object property 
        $StorageDataResult | Add-Member -type NoteProperty -name "File Name" -value $File.Name
        $StorageDataResult | Add-Member -type NoteProperty -name "File Size (KB)" -value ([Math]::Round(($FileSize/1KB),2))
        $StorageDataResult | Add-Member -type NoteProperty -name "Versions Size (KB)" -value ([Math]::Round(($VersionSize/1KB),2))
        $StorageDataResult | Add-Member -type NoteProperty -name "Total File Size (MB)" -value ([Math]::Round(($TotalFileSize/1MB),2))
        $StorageDataResult | Add-Member -type NoteProperty -name "File Type" -value $File.Item['File Type'] 
        $StorageDataResult | Add-Member -type NoteProperty -name "Created on" -value $File.TimeCreated 
        $StorageDataResult | Add-Member -type NoteProperty -name "Last Modified" -value $File.TimeLastModified 
        $StorageDataResult | Add-Member -type NoteProperty -name "Created by" -value $File.Author.Name        
        $StorageDataResult | Add-Member -type NoteProperty -name "Parent Folder" -value $File.ParentFolder

        $StorageDataCollection += $StorageDataResult
        #Write-Host "Processing File:"$File.Name
      #Get Files in Sub Folders
        foreach ($SubFolder in $Folder.SubFolders)
         if($SubFolder.Name -ne "Forms") #Leave "Forms" Folder which has List default Aspx Pages.

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

#Set Site URL and Library name variables
$WebURL = ""
$LibraryName = "Invoices"

#Get the Web where library exists
$Web = Get-SPWeb $WebURL
#Get the Library's Root Folder
$Library =  $Web.Lists[$LibraryName].RootFolder
#Call the function to get library's detailed storage Metrics
$StorageDetails = GetStorageMetrics($Library)

write-host "Total Number of Files in the library:" $Web.Lists[$LibraryName].ItemCount 

#Get the Total size of the library
$TotalSize = ($StorageDetails | Measure-Object 'Total File Size (MB)' -Sum | Select -expand Sum)  
Write-host "Library Size in MB: "([Math]::Round($TotalSize,2))

#export the detailed storage Info the CSV file
$StorageDetails | sort-object "Total File Size (MB)" -descending | Export-csv "LibraryStroageRpt.csv" -notypeinformation
Write-host "Detailed Storage Report has been generated!"

This report will tell you how much space each file is taking up in CSV format, which we can open in Excel and add a few more formatting to get insights into where and what files are occupying space and how we can categorize them.

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. Passionate about sharing the deep technical knowledge and experience to help others, through the real-world articles!

2 thoughts on “Storage Analysis Report for SharePoint Document Library

  • Hi Salaudeen,

    I’m trying to use your script with a Sharepoint Online site, but I’m getting the following errors(i’m Powershell 7.3.2 with admin rights)

    Can you help?

    Kind regards,

    Line |
    55 | $Web = Get-SPWeb $WebURL
    | ~~~~~~~~~
    | The term ‘Get-SPWeb’ is not recognized as a name of a cmdlet, function, script file, or executable program.
    Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
    Line |
    58 | $Library = $Web.Lists[$LibraryName].RootFolder
    | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    | Cannot index into a null array.
    Line |
    63 | write-host “Total Number of Files in the library:” $Web.Lists[$Librar …
    | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    | Cannot index into a null array.
    Line |
    67 | Write-host “Library Size in MB: “([Math]::Round($TotalSize,2))
    | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    | Argument types do not match
    Detailed Storage Report has been generated!
    Line |
    73 | $Web.dispose()
    | ~~~~~~~~~~~~~~
    | You cannot call a method on a null-valued expression.

  • Hi, I tried your script on a SP 2007 farm and there are a few bugs in it. The first was the -name “File Type” -value $File.Item[‘File Type’]. It gave “cannot index into a null array. Took that out of the script. I tried the script again and it will work for smaller document libraries, but nothing that contains over 100K of documents. The error is “script failed due to call depth overflow.” It is not your issue, but error checking would help in case others run into this same problem. Thanks


Leave a Reply

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