Collaborate, Innovate, Automate

Get-VersionReport

This PnP PowerShell script reports version counts for every file in a SharePoint document library. It is a read-only, non-destructive audit — nothing is deleted. Run this script before using Invoke-VersionTrim to understand the scope of version history storage in a library.

Purpose

Version history is enabled by default on every SharePoint document library and grows silently over time. Before committing to any cleanup, this script gives you a clear picture of the current state by:

Prerequisites

Parameters to update

PowerShell Script

Connect-PnPOnline -Url "https://tenantName.sharepoint.com/sites/siteName" -Interactive -ClientId ""

# Get all files in a library and their version counts — DRY RUN, no deletion
$items = Get-PnPListItem -List "SitePages" -Fields "FileLeafRef", "FileRef", "FSObjType"

foreach ($item in $items) {
    # Skip folders (FSObjType = 1), only process files (FSObjType = 0)
    if ($item["FSObjType"] -eq 1) { continue }

    $versions = Get-PnPFileVersion -Url $item["FileRef"]
    Write-Host "$($item["FileLeafRef"]) has $($versions.Count) versions"
}

Usage Notes

Related