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:
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:
- 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.
- 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:
Hi will this work on SPO? I have a tenant using a lot of space up with lots of versions
For SharePoint Online, Refer: Version History Report for SharePoint Online using PowerShell
Do we get the actual versions via script(not just number but real versioned files)?
You can download all versions of documents Download All Versions of a Document in SharePoint using PowerShell
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.
Thanking you, this works great.
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
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?
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)?
Use: $ListItem[“Author”] to get the Owner of the Item.
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