kwizcom banner advertisement

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:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

Function Generate-VersionSizeReport()
{  
    #Define 'Web Application URL' as Mandatory Parameter
    Param(
    [Parameter(Mandatory=$true)] [string]$WebAppURL,
    [Parameter(Mandatory=$true)] [string] $CSVReport
    )
 
    #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 $CSVReport

    #Loop through each site collection
    Foreach($Site in $WebApp.Sites)
    {
        #Loop through each site in the site collection
        Foreach($Web in $Site.AllWebs)
        {
            Write-host "Processing Web:"$Web.Url
            #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 $CSVReport -Append
                            }
                        }
                    }
                }
            }
            $Web.Dispose()          
        }
        $Site.Dispose()          
    }
 
    #Send message to console
    write-host "Versioning Report Generated Successfully!"
}

#Call the Function to Generate Version History Report
Generate-VersionSizeReport -WebAppURL "http://intranet.crescent.com" -CSVReport "C:\Temp\VersioningRpt.csv"

and the output after importing Data to Microsoft Excel:
SharePoint Document Versions Size Report with PowerShell
 After adding Pivot chart for analysis:
versioning report for sharepoint using powershell
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:

PowerShell to Generate Version History Report for SharePoint 2007 
[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 Generate-VersionSizeReport()
{  
    #Define 'Web Application URL' as Mandatory Parameter
    Param(
    [Parameter(Mandatory=$true)] [string]$WebAppURL,
    [Parameter(Mandatory=$true)] [string] $CSVReport
     )
    #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 $CSVReport

    #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 $CSVReport -Append
                            }
                        }
                    }
                }
            }
            $Web.Dispose()          
        }
        $Site.Dispose()          
    }
 
    #Send message to console
    write-host "Versioning Report Generated Successfully!"
}

#Call the Function to Generate Version History Report
Generate-VersionSizeReport -WebAppURL "http://sharepoint.crescent.com" -CSVReport "C:\Temp\VersionHistory.csv"

SharePoint Document Versions Size Report with PowerShell SharePoint Document Versions Size Report with PowerShell Reviewed by Salaudeen Rajack on 7:24 PM Rating: 5

5 comments:

  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
  2. Very useful script Salaudeen Rajack, Can we also get the item owner information on the report (A new column on the report that has Users & groups name that has full control access on the item)?

    ReplyDelete
    Replies
    1. Use: $ListItem["Author"] to get the Owner of the Item.

      Delete
  3. pls , help me out , how to run this script in SharePoint Server's Windows PowerShell script? what and all need to change and where i need to include the URL of the Site wher i want my version report from?

    ReplyDelete
  4. Hi Salaudeen, thanks for script. The data delivered are exactly what we needed. I just realized that the script does not get all items/documents from libraries. I adjusted the script, so that it runs only through one site-collection. you have any idea why it did not get all items?

    thanks

    ReplyDelete

Please Login and comment to get your questions answered!

Powered by Blogger.