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.
             {
                GetStorageMetrics($SubFolder)          
             }
        } 

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

#Set Site URL and Library name variables
$WebURL = "https://sharepoint.crescent.com/finance/"
$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!"
      
$Web.dispose() 

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 - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

One thought on “Storage Analysis Report for SharePoint Document Library

  • 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

    Reply

Leave a Reply

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