How to get document libraries size in your SharePoint Server 2007/2010/2013


How to get every document libraries size in your SharePoint Environment. SP2007/SP2010/SP2013?


 My current client is moving SharePoint On-Prem to SharePoint Online and they asked me to how much some of the document libraries are taking up size on the server. As most of the document library had more than 5000 Items and were very large libraries.
The logic I came out with was
  • 1.       Find every site collection in the SharePoint Server farm
  • 2.       For every Site collection get all the sites.
  • 3.       For every site get all the document libraries
  • 4.       For each document library get all the items attachments length.
  • 5.       Add it up per library and that should give total size on the server as shown below.


Following is the screenshot of output generated.





Following script generates the above output:

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") > $null

function Get-Inventory(){
 $farm = [Microsoft.SharePoint.Administration.SPFarm]::Local
 $websvcs = $farm.Services | where -FilterScript {$_.GetType() -eq [Microsoft.SharePoint.Administration.SPWebService]}
 $webapps = @()

 foreach ($websvc in $websvcs)
 {
     write-host "Web Applications"
     write-host ""
     foreach ($webapp in $websvc.WebApplications)
     {
         write-host "Webapp Name -->"$webapp.Name
         write-host ""
         write-host "Site Collections"
         write-host ""
         foreach ($site in $webapp.Sites)
          {
            write-host "Site URL --> -->" $site.URL
            write-host ""
            write-host "Websites"
            write-host ""
             foreach ($web in $site.AllWebs)
             {
                    write-host ""
                    write-host ""
                    foreach ($list in $web.Lists) {
                        if ($list.BaseType -eq "DocumentLibrary") {
                          if(CheckSystemLibrary($list.Title))
                          {
                                $listSize = 0
                                foreach ($item in $list.items)
                                {
                                    $listSize += ($item.file).length
                                }                               
                                #"Web: "+$web.Title+", Library Name: "+$list.Title+",  "+[Math]::Round(($listSize/1KB),2)+" KB " 
                                $size = [Math]::Round(($listSize/1KB),2) 
                                $line = @{
                                            "Web" = $web.Title
                                            "URL" = $web.URL
                                            "Library"   =  $list.Title
                                            "Size-KB" =  $size
                                         }
                                New-Object PSObject -Property $line | select "Web","URL","Library","Size-KB"                                        
                               
                          } #end if checksystemlibrary      
                        } #foreach list.
                     }                     
             } #web
          } # site
      } # webapp
     } #websvc
   } #function.
  
   function CheckSystemLibrary($sysLib)
   {
       
        $arrLib = "Site Collection Documents", "Site Collection Images", "Site Images", "Site Pages", "Site Template Gallery","Site Templates", "Style Library", "Web Part Gallery",
                "Pages","Site Views","SitePages"
        $found = $arrLib -contains $sysLib
        return -Not $found
   }
  
                cls
                Get-Inventory | Export-Csv -NoTypeInformation -Path C:\Scripts\Output.csv