Document library in SharePoint shows count of the documents only, but there are many minor and major versions for a document. In this blog post I am going to share how we can generate a report for a document library which have details for all major and minor versions of document version size and checkedIn comments.

You can check the size of document library using Storage Metrics but some time it does not show the exact size because its incremental only and always add the document version size to it, you need to reset it manually, check how to reset the SharePoint Storage Metrics size.

You can check the version details for any specific document by going to document version detail and check the version history with version detail, size, checked in comments and user who modified it.

SharePoint Document version history

Document library with large versions also slow down the performance of the document library, I am going to share a PowerShell script which will generate the versioning detail for a complete document library.

SharePoint Documents with versioning detail

The script will generate the report of a document library with all major and minor versions, it have a function named GetAllDocInventoryWithVerions which takes two parameters, the site URL and document library.

function GetAllDocInventoryWithVersions([string]$siteUrl, $documentLibrary) {

$SPWeb = Get-SPWeb $siteUrl #Change the SharePoint site name here#
write-host SPWeb.Title
$List = $SPWeb.Lists[$documentLibrary] #Change the document library name here#
$ItemsColl = $List.Items
$tableau =@();

foreach ($item in $ItemsColl) 
{
    $itemFileSize = $item.File.Length/1KB
    $o = new-object psobject
  
    $o | Add-Member -MemberType noteproperty -Name "ItemID" -value $item.ID;
    $o | Add-Member -MemberType noteproperty -Name "Item Title" -value $item.Title;
    $o | Add-Member -MemberType noteproperty -Name "Version" -value  "";#$item.Versions.Count;
    $o | Add-Member -MemberType noteproperty -Name "Item URL" -value $item.Url;    
    $o | Add-Member -MemberType noteproperty -Name "Comments" -value  $item.CheckInComment;
    $o | Add-Member -MemberType noteproperty -Name "File Size (KB)" -value $itemFileSize ;
    $o | Add-Member -MemberType noteproperty -Name "Created Date" -value $item["Created"]
    $o | Add-Member -MemberType noteproperty -Name "Created By" -value $item["Author"]
    $o | Add-Member -MemberType noteproperty -Name "Modifed Date" -value $item["Created"]
    $o | Add-Member -MemberType noteproperty -Name "Modifed By" -value $item["Editor"]
    $tableau += $o;
    
    foreach($Ver in $item.Versions)
     {
        $verFileSize = $item.File.Versions.GetVersionFromLabel($Ver.VersionLabel).Size/1KB
        $v = new-object psobject
        $comment = $item.File.Versions.GetVersionFromLabel($Ver.VersionLabel).CheckInComment
           
        $v | Add-Member -MemberType noteproperty -Name "ItemID" -value $item.ID;
        $v | Add-Member -MemberType noteproperty -Name "Item Title" -value $item.Title;
        $v | Add-Member -MemberType noteproperty -Name "Version" -value  $Ver.VersionLabel;
        $v | Add-Member -MemberType noteproperty -Name "Item URL" -value $Ver.Url;
        $v | Add-Member -MemberType noteproperty -Name "Comments" -value  $comment;    
        $v | Add-Member -MemberType noteproperty -Name "File Size (KB)" -value $verFileSize;
        $v | Add-Member -MemberType noteproperty -Name "Created Date" -value $Ver["Created"]
        $v | Add-Member -MemberType noteproperty -Name "Created By" -value $Ver["Author"]
        $v | Add-Member -MemberType noteproperty -Name "Modifed Date" -value $Ver["Created"]
        $v | Add-Member -MemberType noteproperty -Name "Modifed By" -value $Ver["Editor"]
        $tableau += $v;
       
     }
}

$SPWeb.Dispose();
#$tableau| Out-GridView
return $tableau
}

You can call the function output either in PowerShell gridview or in a CSV file. Run below cmdlet to execute the output in a gridview

GetAllDocInventoryWithVersion "https://SharePoint2013" "Program Management"| Out-GridView

To save the report in excel/csv, run the below command which will save the file at given location.

GetAllDocInventoryWithVersion "https://SharePoint2013" "Program Management" Out-File "E:\Reports\DocumentLibraryInventory.csv"

How to get CheckedIn comment

In above script, you need to get the checkedIn comment from the document version detail as Iisted in above script.

$comment = $item.File.Versions.GetVersionFromLabel($Ver.VersionLabel).CheckInComment

How to get Size for a specific version

Some time you want to know the size of a specific version, in above script I used the GetVersionsFromLabel method to get the size of a specific version. You can load any specific document using PowerShell and can get the version size.

$verFileSize = $item.File.Versions.GetVersionFromLabel($Ver.VersionLabel).Size/1KB

Deleting document version using PowerShell

You can delete version directly from version history or using PowerShell script, powershell script will be helpful if you need to delete multiple versions.

$Ver.Delete()

$Ver here is the document version which I have looped in above script.

Adnan is six time Microsoft MVP (Since 2015) with over 16 years of extensive experience with major expertise on SharePoint, SharePoint based development, Microsoft 365, Microsoft Teams, .Net Platform and Microsoft BI. He is currently working Sr Microsoft Consultant at Olive + Goose. He is MCT Regional Lead for Pakistan Chapter since 2012. He is working on SharePoint for past 12 years and worked on different intranet/intranet solutions for private & govt. sector majorly in United states and Gulf region and have experience of working with multiple Fortune 500 companies. He is a trainer, technology evangelist and also speaks in community forums.

Leave a Reply