Skip to content

Export Sharepoint Documents Version History Count

Updated: at 09:30 PM

This is one of my favorite scripts for Sharepoint. It will export a Sharepoint Document library by version history count. It will list all files and display the version count, file size, created and last modified date. This script can be used in-conjunction with other scripts to help clean up your libraries.

PowerShell Script

This script will use the Pnp.PowerShell module - you can install this via

Install-Module PnP.PowerShell -RequiredVersion 1.12.0 -Force

Then we set our variables for the script, defining the Sharepoint Site & Library Name and the path to export the exported data to.

$SiteURL = "https://contoso.sharepoint.com/sites/Technical"
$LibraryName = "Documents"
$CSVPath = "C:\PowershellCSVs\SPsiteTechnicalExport.csv"

# Connect to SharePoint Online site
Connect-PnPOnline -Url $SiteURL -Interactive

$VersionHistoryData = @()

This next section iterates through each item in the document library, retrieving the files size, versions, created and last modified dates. I’ve implemented a start-sleep -Milliseconds 70 limit to reduce the risk of reaching Microsofts API requests per minute.

Get-PnPListItem -List $LibraryName -PageSize 500 | Where {$_.FieldValues.FileLeafRef -like "*.*"} | ForEach-Object {
    Write-Host "Getting Versioning Data of the File:" $_.FieldValues.FileRef
    Start-Sleep -Milliseconds 70

    # Get FileSize, version Size, creation date, and last modified date
    $FileSizeinKB = [Math]::Round(($_.FieldValues.File_x0020_Size / 1KB), 2)
    $File = Get-PnPProperty -ClientObject $_ -Property File
    $Versions = Get-PnPProperty -ClientObject $File -Property Versions
    $VersionSize = $Versions | Measure-Object -Property Size -Sum | Select-Object -ExpandProperty Sum
    $VersionSizeinKB = [Math]::Round(($VersionSize / 1KB), 2)
    $TotalFileSizeKB = [Math]::Round(($FileSizeinKB + $VersionSizeinKB), 2)
    $Created = $File.TimeCreated
    $LastModified = $File.TimeLastModified

Finally, the data is added into an array and exported into a CSV file with the path we defined in the variable at the start.

# Extract Version History data
    $VersionHistoryData += New-Object PSObject -Property ([Ordered]@{
        "File Name" = $_.FieldValues.FileLeafRef
        "File URL" = $_.FieldValues.FileRef
        "Versions" = $Versions.Count
        "File Size (KB)" = $FileSizeinKB
        "Version Size (KB)" = $VersionSizeinKB
        "Total File Size (KB)" = $TotalFileSizeKB
        "Created Date" = $Created
        "Last Modified Date" = $LastModified
    })
}

$VersionHistoryData | Format-Table -AutoSize
$VersionHistoryData | Export-Csv -Path $CSVPath -NoTypeInformation

The complete script:

$SiteURL = "https://contoso.sharepoint.com/sites/Technical"
$LibraryName = "Documents"
$CSVPath = "C:\PowershellCSVs\SPsiteTechnicalExport.csv"

# Connect to SharePoint Online site
Connect-PnPOnline -Url $SiteURL -Interactive

$VersionHistoryData = @())

Get-PnPListItem -List $LibraryName -PageSize 500 | Where {$_.FieldValues.FileLeafRef -like "*.*"} | ForEach-Object {
    Write-Host "Getting Versioning Data of the File:" $_.FieldValues.FileRef
    Start-Sleep -Milliseconds 70

    # Get FileSize, version Size, creation date, and last modified date
    $FileSizeinKB = [Math]::Round(($_.FieldValues.File_x0020_Size / 1KB), 2)
    $File = Get-PnPProperty -ClientObject $_ -Property File
    $Versions = Get-PnPProperty -ClientObject $File -Property Versions
    $VersionSize = $Versions | Measure-Object -Property Size -Sum | Select-Object -ExpandProperty Sum
    $VersionSizeinKB = [Math]::Round(($VersionSize / 1KB), 2)
    $TotalFileSizeKB = [Math]::Round(($FileSizeinKB + $VersionSizeinKB), 2)
    $Created = $File.TimeCreated
    $LastModified = $File.TimeLastModified

    # Extract Version History data
    $VersionHistoryData += New-Object PSObject -Property ([Ordered]@{
        "File Name" = $_.FieldValues.FileLeafRef
        "File URL" = $_.FieldValues.FileRef
        "Versions" = $Versions.Count
        "File Size (KB)" = $FileSizeinKB
        "Version Size (KB)" = $VersionSizeinKB
        "Total File Size (KB)" = $TotalFileSizeKB
        "Created Date" = $Created
        "Last Modified Date" = $LastModified
    })
}

$VersionHistoryData | Format-Table -AutoSize
$VersionHistoryData | Export-Csv -Path $CSVPath -NoTypeInformation

✨ Feedback & Suggestions

If you have any suggestions/feedback, you can contact me via my email.