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 a 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 "https://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 "https://sharepoint.crescent.com" -CSVReport "C:\Temp\VersionHistory.csv"

Get Versions Size of a Document Library using PowerShell:

#Define Variables
$SiteURL="https://opera.crescent.com/sites/sales/"
$ListName = "Shared Documents"
$TotalSize = 0

#Calculate the Versions Size of the List
Get-SPWeb $WebURL | Select -ExpandProperty Lists | Where { $_.Title  -eq $ListName } | 
    Select -ExpandProperty Items | Select -ExpandProperty File | Select -ExpandProperty Versions | 
        ForEach { $TotalSize = $TotalSize + $_.Size }

Write-host "Total Versions Size: $($TotalSize/1MB) MB"

For SharePoint Online, Refer:

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

12 thoughts on “SharePoint Document Versions Size Report with PowerShell

  • Is there a SharePoint Online version report like this?

    Reply
  • Hi will this work on SPO? I have a tenant using a lot of space up with lots of versions

    Reply
  • Do we get the actual versions via script(not just number but real versioned files)?

    Reply
  • Thanks for pointing me to this. My issue is I need to get a version history report for all “document sets”, not documents. It seems the versions for document sets are stored in an entirely different way and I can’t find any examples online to get me started.

    Reply
  • Thanking you, this works great.

    Reply
  • 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

    Reply
  • 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?

    Reply
  • 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)?

    Reply
  • 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

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *