Sunday, January 20, 2013

SharePoint Document Versions Size Report with PowerShell

This is a PowerShell version of my existing post Version History Size Report for SharePoint which uses C# object model to generate versioning report and gives insights, such as:

  • Site/Library's Total versions size
  • Total No. of versions created
  • Which documents are with more versions
  • Type of the documents
  • Library in which the document stored. Library Size with/Without versions
  • When was the last version modified
  • Size of the document's latest version
  • How much storage space being occupied by versions
  • Total size of the document including versions
PowerShell Script to generate Document Versions Report:
# Get Size of all Sub-sites in a Site Collection
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

#Region MOSS2007-CmdLets

Function Get-SPWebApplication()
{   
  Param( [Parameter(Mandatory=$true)] [string]$WebAppURL )
  return [Microsoft.SharePoint.Administration.SPWebApplication]::Lookup($WebAppURL)
}

Function global:Get-SPSite()
{
  Param( [Parameter(Mandatory=$true)] [string]$SiteCollURL )

   if($SiteCollURL -ne '')
    {
    return new-Object Microsoft.SharePoint.SPSite($SiteCollURL)
   }
}
 
Function global:Get-SPWeb()
{
 Param( [Parameter(Mandatory=$true)] [string]$SiteURL )
  $site = Get-SPSite($SiteURL)
        if($site -ne $null)
            {
               $web=$site.OpenWeb();
            }
    return $web
}
#EndRegion

 Function GenerateVersionSizeReport()
 {  
    #Define 'Web Application URL' as Mandatory Parameter
    Param( [Parameter(Mandatory=$true)] [string]$WebAppURL )
 
 #Get the Web Application
    $WebApp=Get-SPWebApplication($WebAppURL)

    #Write the CSV Header - Tab Separated
 "Site Name`t Library `t File Name `t File URL `t File Type `t Last Modified `t No. of Versions `t Latest Version Size(MB) `t Versions Size(MB) `t Total File Size(MB)" | out-file VersionSizeReport.csv

 #Loop through each site collection
  foreach($Site in $WebApp.Sites)
   {
    #Loop through each site in the site collection
     foreach($Web in $Site.AllWebs)
   {
            #Loop through  each List
            foreach ($List in $Web.Lists)
            {
                #Get only Document Libraries & Exclude Hidden System libraries
                if ( ($List.BaseType -eq "DocumentLibrary") -and ($List.Hidden -eq $false) )
                {
                    foreach ($ListItem  in $List.Items)
                    {
          #Consider items with 1+ versions
                        if ($ListItem.Versions.Count -gt 1)
                        {
          $versionSize=0

                            #Get the versioning details
                            foreach ($FileVersion in $ListItem.File.Versions)
                            {
                                $versionSize = $versionSize + $FileVersion.Size;
                            }
       #To Calculate Total Size(MB)
       $ToalFileSize= [Math]::Round(((($ListItem.File.Length + $versionSize)/1024)/1024),2)
       
                            #Convert Size to MB
                            $VersionSize= [Math]::Round((($versionSize/1024)/1024),2)
       
       #Get the Size of the current version
       $CurrentVersionSize= [Math]::Round((($ListItem.File.Length/1024)/1024),2)
       
                            #Get Site Name
                            if ($Web.IsRootWeb -eq $true)
                            {
                                $siteName = $Web.Title +" - Root";
                            }
                            else
                            {
                                $siteName= $Site.RootWeb.Title + " - " + $Web.Title;
                            }

                            #Log the data to a CSV file where versioning size > 0MB!
                            if ($versionSize -gt 0)
                            {
                                "$($siteName) `t $($List.Title) `t $($ListItem.Name) `t $($Web.Url)/$($ListItem.Url) `t $($ListItem['File Type'].ToString()) `t $($ListItem['Modified'].ToString())`t $($ListItem.Versions.Count) `t $CurrentVersionSize `t $($versionSize) `t $($ToalFileSize)" | Out-File VersionSizeReport.csv -Append
                            }
                        }
                    }
                }
            }
  $Web.Dispose()          
        }
 $Site.Dispose()          
    }
 
    #Send message to console
    write-host "Versioning Report Generated Successfully!"
}

#Call the Function to Generate Version History Report
GenerateVersionSizeReport "http://sharepoint.crescent.com"

and the output after importing Data to Microsoft Excel:
 After adding Pivot chart for analysis:

You can download the script from MSDN: SharePoint Document Versions Size Report for SharePoint 2010 and MOSS 2007

Few Additions to the Script:
  1. Exclude SharePoint's System Lists & Libraries: You may want to exclude Lists and Libraries SharePoint uses for its own operations. E.g. "Pages". Just have a array with all system lists and exclude them from processing. 
  2. You may want to compare date. Such as: consider documents which are more than one year old (in other words, Last modified date > 1 year from now)
Here is the updated script, satisfying above requirements:
# Get Size of all Sub-sites in a Site Collection
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") > $null

#Region MOSS2007-CmdLets

Function Get-SPWebApplication()
{   
  Param( [Parameter(Mandatory=$true)] [string]$WebAppURL )
  return [Microsoft.SharePoint.Administration.SPWebApplication]::Lookup($WebAppURL)
}

Function global:Get-SPSite()
{
  Param( [Parameter(Mandatory=$true)] [string]$SiteCollURL )

   if($SiteCollURL -ne '')
    {
    return new-Object Microsoft.SharePoint.SPSite($SiteCollURL)
   }
}
 
Function global:Get-SPWeb()
{
 Param( [Parameter(Mandatory=$true)] [string]$SiteURL )
  $site = Get-SPSite($SiteURL)
        if($site -ne $null)
            {
               $web=$site.OpenWeb();
            }
    return $web
}
#EndRegion

 Function GenerateVersionSizeReport()
 {  
    #Define 'Web Application URL' as Mandatory Parameter
    Param( [Parameter(Mandatory=$true)] [string]$WebAppURL )
 
 #Get the Web Application
    $WebApp=Get-SPWebApplication($WebAppURL)

    #Write the CSV Header - Tab Separated
 "Site Collection Name `t Site Name`t Library `t File Name `t File URL `t File Type `t Last Modified `t No. of Versions `t Latest Version Size(MB) `t Versions Size(MB) `t Total File Size(MB)" | out-file VersionSizeReport.csv

 #Arry to Skip System Lists and Libraries
 $SystemLists =@("Pages", "Converted Forms", "Master Page Gallery", "Customized Reports", "Documents", 
                 "Form Templates", "Images", "List Template Gallery", "Theme Gallery", "Reporting Templates", 
         "Site Collection Documents", "Site Collection Images", "Site Pages", "Solution Gallery", 
                               "Style Library", "Web Part Gallery","Site Assets", "wfpub")
 
 #Get Last Year's Same day!
 $DateFilter=([DateTime]::Now.AddYears(-1))

   #Loop through each site collection
  foreach($Site in $WebApp.Sites)
   {
    #Loop through each site in the site collection
     foreach($Web in $Site.AllWebs)
   {
            #Loop through  each List
            foreach ($List in $Web.Lists)
            {
                #Get only Document Libraries & Exclude Hidden System libraries
                if ( ($List.BaseType -eq "DocumentLibrary") -and ($List.Hidden -eq $false) -and($SystemLists -notcontains $List.Title) )
                {
                    foreach ($ListItem  in $List.Items)
                    {
          #Consider items with 5+ versions And apply Date Filter
                        if ( ($ListItem.Versions.Count -gt 5) -and ( $ListItem['Modified'] -lt $DateFilter))
                        {
          $versionSize=0

                            #Get the versioning details
                            foreach ($FileVersion in $ListItem.File.Versions)
                            {
                                $versionSize = $versionSize + $FileVersion.Size;
                            }
       #To Calculate Total Size(MB)
       $ToalFileSize= [Math]::Round(((($ListItem.File.Length + $versionSize)/1024)/1024),2)
       
                            #Convert Size to MB
                            $VersionSize= [Math]::Round((($versionSize/1024)/1024),2)
       
       #Get the Size of the current version
       $CurrentVersionSize= [Math]::Round((($ListItem.File.Length/1024)/1024),2)

                            #Log the data to a CSV file where versioning size > 0MB!
                            if ($versionSize -gt 0)
                            {
                                "$($Site.RootWeb.Title) `t $($Web.Title) `t $($List.Title) `t $($ListItem.Name) `t $($Web.Url)/$($ListItem.Url) `t $($ListItem['File Type'].ToString()) `t $($ListItem['Modified'].ToString())`t $($ListItem.Versions.Count) `t $CurrentVersionSize `t $($versionSize) `t $($ToalFileSize)" | Out-File VersionSizeReport.csv -Append
                            }
                        }
                    }
                }
            }
  $Web.Dispose()          
        }
 $Site.Dispose()          
    }
 
    #Send message to console
    write-host "Versioning Report Generated Successfully!"
}

#Call the Function to Generate Version History Report
GenerateVersionSizeReport "http://sharepoint.crescent.com"



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Five Challenges in SharePoint Security
...And How to Solve Them. Free White Paper
*Sponsored


Check out these SharePoint products:

1 comment :

  1. while runnning the report I got the following error, but the report still ran ofcourse:
    You cannot call a method on a null-valued expression.
    At line:182 char:32
    + $ListItem['File Type'].ToString <<<< ()
    + CategoryInfo : InvalidOperation: (ToString:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

    ReplyDelete

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...