How to Get the Size of the Document with and Without Version in SharePoint using PowerShell Script – Server-Side Object Model

Sathish Nadarajan
 
Solution Architect
March 13, 2018
 
Rate this article
 
Views
5245

Recently faced an interesting requirement to get the Size of the Documents with and Without Version during the preparation of Inventory in one of the Migration assignment.

Usually, we can get the size of the Web Site from the admin console itself. But, it will give you the size including the Versions of the documents. But if the customer wants to migrate only the final version, then the size may not be realistic.

To be precise, if the size of the Web shows as 20 GB in the Console, and the requirement is to migrate only the final version, then the content migrated would be only 5 GB. (The numbers which I am referring are for example purpose only.) Hence, the remaining 15 GB, we don’t event consider for our planning.

The below piece of PowerShell Script will give you both the Options.

 function GetSPFolderSize ($Folder)
 {
    $byteCount = 0   
          
         # calculate the files in the immediate folder    
 
         foreach ($SPFile in $Folder.Files)    
         {       
             $byteCount += $SPFile.TotalLength 
 
             # The below line will include the versions  
 
               foreach ($SPFileVersion in $SPFile.Versions)   
               {              
                 $byteCount += $SPFileVersion.Size   
               }
               
              
         }        
         # Handle sub folders     
         foreach ($SubFolder  in $Folder.SubFolders)    
         {       
             $byteCount += GetSPFolderSize  $SubFolder
         }        
     return $byteCount  
 }
 

To call the method,

 $webBytes = GetSPFolderSize  $Web.RootFolder
 

The $webBytes variable will contain the size in Bytes.

Happy Coding,

Sathish Nadarajan.

Category : PowerShell, SharePoint

Author Info

Sathish Nadarajan
 
Solution Architect
 
Rate this article
 
Sathish is a Microsoft MVP for SharePoint (Office Servers and Services) having 15+ years of experience in Microsoft Technologies. He holds a Masters Degree in Computer Aided Design and Business ...read more
 

Leave a comment