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:
- Iterating over every file in the target library
- Skipping folders so only files are reported
- Outputting each file name alongside its current version count
- Making no changes — safe to run in production at any time
Prerequisites
- PnP PowerShell module installed
- Site collection administrator permissions
- A registered Entra ID app with appropriate SharePoint permissions and a Client ID
Parameters to update
- Url — your site URL in the format
https://tenantName.sharepoint.com/sites/siteName - ClientId — your registered Entra ID app Client ID
- -List — the internal name of the library to audit (e.g.
Documents,SitePages)
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
- Change
SitePagesto the internal name of the library you want to audit - On very large libraries,
Get-PnPFileVersioncan be slow — consider running during off-peak hours - Output can be piped to a CSV by wrapping the loop body with
Export-Csvif you need a report to share - Once you have reviewed the output, use Invoke-VersionTrim to act on it
Related
- Invoke-VersionTrim — delete versions beyond a defined threshold using the same library scope
- SharePoint Storage — Fundamentals and Version History — background on why version history is a major storage driver