none
Powershell Script to find document library size where versions are enabled RRS feed

  • Question

  • Hi All,

    I have a client requirement to find the document libraries size where the versions are enable. Is there a way to find using powershell script?

    I have a powershell script which will return all the document libraries where the versions are enabled

    [System.reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint”)
    [System.Reflection.Assembly]::Load(“Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c”)
    [System.Reflection.Assembly]::Load(“Microsoft.SharePoint.Portal, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c”)
    
    
    Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue
    
    #Set variables 
    $siteURL = "http://mypc:2000/SitePages/Home.aspx" 
    $site = new-object Microsoft.SharePoint.SPSite($siteURL)
    
    foreach ($web in $site.AllWebs)
    {
    foreach ($list in $web.Lists)
     {
    if($list.BaseType -eq "DocumentLibrary") 
       {
    
       if($list.EnableVersioning -eq $TRUE)
           {              
                
              $list.Title +"------------" + $web.Url >> D:\Versions\Versionlists.txt  
              }
         }
      }
    }


    Mohammed


    • Edited by MercuryMan Tuesday, October 23, 2012 2:19 PM
    Tuesday, October 23, 2012 2:17 PM

Answers

  • The other alternative would be summing the sizes of the files in the document libraries that have versioning enabled. Something to this effect...

    $site = Get-SPSite http://url/
    
    foreach ($web in $site.AllWebs) {
      foreach ($list in $web.Lists) {
        if($list.BaseType -eq "DocumentLibrary" -and $list.EnableVersioning -eq $TRUE) {  
          $listSize = 0                
          foreach ($item in $list.items) { 
            $listSize += ($item.file).length
          }
          "Web: "+$web.Title+", Library Name: "+$list.Title+", Size: "+[Math]::Round(($listSize/1KB),2)+"KB"
        }
      }
    }
    
    $site.dispose()
    Tuesday, October 23, 2012 3:32 PM
  • Hi Tim,

    Your above code worked for me... I have done a slight change which will give result in .txt file and also the code will exclude default library called "Master Page Gallery". Please see the below code and hope it will help for others.

    [System.reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint”)
    [System.Reflection.Assembly]::Load(“Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c”)
    [System.Reflection.Assembly]::Load(“Microsoft.SharePoint.Portal, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c”)
    
    
    Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue
    
    #Set variables 
    $siteURL = "http://Mypc:30000/" 
    $site = new-object Microsoft.SharePoint.SPSite($siteURL)
    
    
    foreach ($web in $site.AllWebs)
    {
      foreach ($list in $web.Lists)
       {
    
    	if($list.BaseType -eq "DocumentLibrary" -and $list.EnableVersioning -eq $TRUE -and $list.BaseTemplate -notmatch "Catalog")   
            {
    	    $listSize = 0
    	   foreach ($item in $list.items) 
                { 
                  $listSize += ($item.file).length
                }
    	     "Web: "+$web.Title+", Library Name: "+$list.Title+", Size: "+[Math]::Round(($listSize/1KB),2)+"KB" >> D:\Ahmed\DocLibSize\sizereport1.txt	
    	}
        }
    }


    Mohammed

    Thursday, October 25, 2012 12:26 PM

All replies

  • As discussed in this thread there is no direct way to access the size of your document library without looping through all SPFiles & versions.  They do mention a technique using SPSite.StorageManagementInformation that seems to work great - here is a write-up of it:  http://www.jonthenerd.com/2011/08/16/sharepoint-2010-findinglargestdoclibrary/

    The method is marked as obsolete but it still works fine for me.  Here is the example I used to verify:

    $site = Get-SPSite "http://yoursitecollectionortifneeded"; 
    $dataTable = $site.StorageManagementInformation(2,0x11,0,5); 
    $dataTable | ft Title, Size
    

    You'd probably want to pull back more results in your DataTable and then as you're looping through document libraries do a lookup by list id.

    Hope that helps!

    Tuesday, October 23, 2012 2:48 PM
  • The other alternative would be summing the sizes of the files in the document libraries that have versioning enabled. Something to this effect...

    $site = Get-SPSite http://url/
    
    foreach ($web in $site.AllWebs) {
      foreach ($list in $web.Lists) {
        if($list.BaseType -eq "DocumentLibrary" -and $list.EnableVersioning -eq $TRUE) {  
          $listSize = 0                
          foreach ($item in $list.items) { 
            $listSize += ($item.file).length
          }
          "Web: "+$web.Title+", Library Name: "+$list.Title+", Size: "+[Math]::Round(($listSize/1KB),2)+"KB"
        }
      }
    }
    
    $site.dispose()
    Tuesday, October 23, 2012 3:32 PM
  • Hi Tim,

    Your above code worked for me... I have done a slight change which will give result in .txt file and also the code will exclude default library called "Master Page Gallery". Please see the below code and hope it will help for others.

    [System.reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint”)
    [System.Reflection.Assembly]::Load(“Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c”)
    [System.Reflection.Assembly]::Load(“Microsoft.SharePoint.Portal, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c”)
    
    
    Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue
    
    #Set variables 
    $siteURL = "http://Mypc:30000/" 
    $site = new-object Microsoft.SharePoint.SPSite($siteURL)
    
    
    foreach ($web in $site.AllWebs)
    {
      foreach ($list in $web.Lists)
       {
    
    	if($list.BaseType -eq "DocumentLibrary" -and $list.EnableVersioning -eq $TRUE -and $list.BaseTemplate -notmatch "Catalog")   
            {
    	    $listSize = 0
    	   foreach ($item in $list.items) 
                { 
                  $listSize += ($item.file).length
                }
    	     "Web: "+$web.Title+", Library Name: "+$list.Title+", Size: "+[Math]::Round(($listSize/1KB),2)+"KB" >> D:\Ahmed\DocLibSize\sizereport1.txt	
    	}
        }
    }


    Mohammed

    Thursday, October 25, 2012 12:26 PM
  • Glad to hear it helped you! Please mark as helpful or answered :)

    • Edited by Tim Newbill Thursday, October 25, 2012 8:56 PM
    Thursday, October 25, 2012 3:30 PM