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, a distinguished professional, boasts an impressive track record as a Microsoft MVP, having achieved this prestigious recognition for the eighth consecutive year since 2015. With an extensive career spanning over 18 years, Adnan has honed his expertise in various domains, notably excelling in SharePoint, Microsoft 365, Microsoft Teams, the .Net Platform, and Microsoft BI. Presently, he holds the esteemed position of Senior Microsoft Consultant at Olive + Goose. Notably, Adnan served as the MCT Regional Lead for the Pakistan Chapter from 2012 to 2017, showcasing his leadership and commitment to fostering growth within the tech community. His journey in the realm of SharePoint spans 14 years, during which he has undertaken diverse projects involving both intranet and internet solutions for both private and government sectors. His impact has transcended geographical boundaries, leaving a mark on projects in the United States and the Gulf region, often collaborating with Fortune 500 companies. Beyond his roles, Adnan is a dedicated educator, sharing his insights and knowledge as a trainer. He also passionately advocates for technology, frequently engaging with the community through speaking engagements in various forums. His multifaceted contributions exemplify his dedication to the tech field and his role in driving its evolution.

Leave a Reply