Getting insights on total number of version available for each SharePoint list/library item gives you an idea how often an item is getting updated and also helps to know how much space it took (because each version make new stroage space, example item with 4 MB attachment have 5 version then total size that item occupies is 5*4=20MB).
Here we provided site name and list/library names in a CSV file. list names are separated by #
The output will generate a CSV file with File path to SharePoint item and Version count.
function Get-VersionCount() {
try {
$fileLabel = Get-Date -format "yyyyMMdd-HHmm-ss"
$fileLabel += "_VersionCount"
$sitesLibs = Import-Csv -Path $PSScriptRootsiteLib.csv
Add-Content -Path $PSScriptRoot$fileLabel.csv -Value '"FilePath","VersionCount"'
Write-Host $sitesLibs
$sitesLibs |
ForEach-Object {
$siteURL = $_.siteurl
Write-Host "PNP Connection to " $siteURL -ForegroundColor Green
Connect-PnPOnline -Url $siteURL -AppId $env:SPO_AppID -AppSecret $env:SPO_AppSecret
Write-Host "`n"
Write-Host "Executing for " $siteURL -ForegroundColor green "`n `n"
# Loop through Libraries
$libraryArray = $_.libraries.Split("#")
$libraryArray | ForEach-Object {
$verCountObj = @()
$libraryName = $_
Write-Host "Checking Library - " $libraryName "`n" -ForegroundColor Green
$query = "<View Scope='RecursiveAll'><RowLimit>5000</RowLimit></View>"
$listItems = Get-PnPListItem -List $libraryName -Query $query</pre>
$ctx = Get-PnPContext
foreach ($item in $listItems) {
$versionColl = $item.Versions
$ctx.Load($item)
$ctx.Load($versionColl)
$ctx.ExecuteQuery()
$versionsCount = $versionColl.Count
$filePath = $item["FileRef"]
Write-Host "File Name - $($filePath) "
Write-Host "File Path - " $filePath "Version Count - " $versionsCount -ForegroundColor Green
$verCountObj += "$filePath,$versionsCount"
}
$verCountObj += ""
$verCountObj | ForEach-Object { Add-Content -Path $PSScriptRoot$fileLabel.csv -Value $_ }
}
Disconnect-PnPOnline
}
}
catch {
Disconnect-PnPOnline
Write-Host "Exception Type: $($_.Exception.GetType().FullName)"
Write-Host "Exception Message: $($_.Exception.Message)"
}
}
Get-VersionCount
Happy Coding
Fazil
Leave a comment