Storage Analysis Report for SharePoint Document Library

Requirement: On a very large document library, We needed to analyze all documents stored, in-order-to move documents into individual libraries/folders, as this larger library causing latency issues. Lets use PowerShell script to analyze and generate report on document library storage information:

PowerShell script to generate storage metrics report on SharePoint library:
#Get Size of all Sub-sites in a Site Collection
#For MOSS 2007 compatibility
Function global:Get-SPWeb($url)
  $site= New-Object Microsoft.SharePoint.SPSite($url)
        if($site -ne $null)
    return $web
# 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 script gives data back in CSV format,which we can open in Excel, add few more formatting to get the insights on where and what files occupying space and how we can categorize them.

1 comment:

  1. 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


Please Login and comment to get your questions answered!

Powered by Blogger.