Microsoft 365 – PnP PowerShell – Navigating to all the versions of document and exporting respective metadata in CSV file

Hi All,
Greetings for the day!!!
Today one more PowerShell script in our PowerShell bucket 🙂
Here we are discussing PowerShell script which will navigate through all the documents / list items (excluding folders) of a given library and their respective versions, get all metadata and export to CSV file
Here we are using PnP PowerShell
Detailed Steps
- Configure parameters – Site URL, Library Name, CSV file path where we need to save exported metadata
#site url
$SiteURL = "https://knowledgejunction1.sharepoint.com"
#library name
$ListName = "Documents"
#CSV file path
$CSVPath = "C:\Temp\librarydata.csv"
#intialising array object to hold versions metadata of all objects
$VersionHistoryData = @()
- Connect to the site from which document library’s items version details we need to read using – Connect-PnPOnline
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)
- Get the web using – Get-PnPWeb
#Get the Web
$Web = Get-PnPWeb
- Next step is, get all list items from the given library excluding folders using – Get-PnPListItem
#Get All List Items - Exclude Folders
Get-PnPListItem -List $ListName | Where {$_.FileSystemObjectType -ne "Folder"}
- We will iterate through each item and get versions using – Get-PnPProperty
Write-host "Getting Versioning Data of Item:"$_.ID
$Versions = Get-PnPProperty -ClientObject $_ -Property Versions
- We will iterate through each version and fetch the metadata of each version as
ForEach($Version in $Versions)
{
#creating a object to hold the version metadata
$VersionHistory = New-Object PSObject
#version number
$VersionHistory | Add-Member -MemberType NoteProperty -Name "Version Number" -Value $Version.VersionId
#version label
$VersionHistory | Add-Member -MemberType NoteProperty -Name "Version Label" -Value $Version.VersionLabel
#created on
$VersionHistory | Add-Member -MemberType NoteProperty -Name "Created On" -Value (Get-Date ($Version.Created) -Format "yyyy-MM-dd/HH:mm:ss")
#item name
$VersionHistory | Add-Member -MemberType NoteProperty -name "Item Name" -Value $Version["FileRef"]
#content type
$VersionHistory | Add-Member -MemberType NoteProperty -name "Content type" -Value $Version["ContentType"]
#author
$FieldValue = $Version["Author"].LookupValue -join "; "
$VersionHistory | Add-Member -MemberType NoteProperty -name "Author" -Value $FieldValue
#adding current documents version history metadata to array object
$VersionHistoryData+=$VersionHistory
}#ForEach($Version in $Versions)
- Export all versions history data to CSV file
$VersionHistoryData | Export-Csv $CSVPath -NoTypeInformation
- We have detailed article on exporting data to CSV file. Kindly please have a look – PowerShell tutorial – Write a CSV file with PowerShell using the Export-CSV
- On successful execution of script, “librarydata.csv” file will be generated in Temp folder as

- Lets see the CSV file – I have converted it into excel file as

Complete Script
#Config Parameter
#site url
$SiteURL = "https://knowledgejunction1.sharepoint.com"
#library name
$ListName = "Documents"
#CSV file path
$CSVPath = "C:\Temp\librarydata.csv"
#intialising array object to hold versions metadata of all objects
$VersionHistoryData = @()
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)
#Get the Root Web
$Web = Get-PnPWeb
#Get All List Items - Exclude Folders
Get-PnPListItem -List $ListName | Where {$_.FileSystemObjectType -ne "Folder"} | ForEach-Object {
Write-host "Getting Versioning Data of Item:"$_.ID
$Versions = Get-PnPProperty -ClientObject $_ -Property Versions
ForEach($Version in $Versions)
{
$VersionHistory = New-Object PSObject
$VersionHistory | Add-Member -MemberType NoteProperty -Name "Version Number" -Value $Version.VersionId
$VersionHistory | Add-Member -MemberType NoteProperty -Name "Version Label" -Value $Version.VersionLabel
$VersionHistory | Add-Member -MemberType NoteProperty -Name "Created On" -Value (Get-Date ($Version.Created) -Format "yyyy-MM-dd/HH:mm:ss")
#Append Version data
#$VersionHistory | Add-Member -MemberType NoteProperty -Name $_.InternalName -Value $FieldValue
$VersionHistory | Add-Member -MemberType NoteProperty -name "Number of Version" -Value $Versions.Count
$VersionHistory | Add-Member -MemberType NoteProperty -name "Item Name" -Value $Version["FileRef"]
$VersionHistory | Add-Member -MemberType NoteProperty -name "Content type" -Value $Version["ContentType"]
$FieldValue = $Version["Author"].LookupValue -join "; "
$VersionHistory | Add-Member -MemberType NoteProperty -name "Author" -Value $FieldValue
$VersionHistoryData+=$VersionHistory
}
}
$VersionHistoryData | Export-Csv $CSVPath -NoTypeInformation
If you have any issue in PowerShell, kindly please feel free to discuss. We are good at Microsoft 365 PowerShell 🙂
Thanks for reading ! HAVE a FANTASTIC LEARNING 🙂 LIFE IS BEAUTIFUL 🙂
If you think article worth, kindly please share and like !
You must be logged in to post a comment.