SharePoint Diary posted a script for generating a report for all versions of a document in a document library. I needed something similar, but not an itemized list. I needed a summary report for all SharePoint online sites listed in a CSV. So I modified the script to accomplish that. This script reads in a list of URLs from a CSV and report out a size list for each site for files, versions, and combined.
#Set Variables
$sitelist = Import-CSV C:\SWSetup\SPOSiteList.csv
$SPAdminURL = "https://contoso-admin.sharepoint.com"
Connect-PnPOnline -Url $SPAdminURL -Interactive
#Exclude certain libraries
$ExcludedLists = ("Preservation Hold Library","Site Assets","Site Pages","Style Library","Form Templates")
$datestring = (Get-Date).ToString(“s”).Replace(“:”,”-”)
$CSVPath = "C:\SWSetup\VersionSizeSummary_" + $($datestring) +".csv"
"SiteURL `t File Size (GB) `t Version Size (GB) `t Total File Size (GB)" | Out-File $CSVPath -Append
Function Enable-PnPVersionReport($SiteURL)
{
#Connect to SharePoint Online site
Connect-PnPOnline -Url $SiteURL -Interactive
$FileSizeinGB = 0
$VersionSize = 0
$VersionSizeinGB = 0
$TotalFileSizeGB = 0
$Lists = Get-PnPList | Where-Object {$_.Hidden -eq $False -and $_.BaseType -eq "DocumentLibrary" -and $ExcludedLists -notcontains $_.Title}
Foreach ($l in $Lists)
{
#Write-host "Connecting to $($l.Title)" -f Magenta
$items = Get-PnPListItem -List $($l.Title)
$VersionHistoryData = @()
#Iterate through all files
Get-PnPListItem -List $l -PageSize 500 | Where {$_.FieldValues.FileLeafRef -like "*.*" -and [string]::IsNullOrEmpty($_.FieldValues.File_x0020_Type) -ne "True" } | ForEach-Object {
#Write-host "Getting Versioning Data of the File:"$_.FieldValues.FileRef -f Green
#Get FileSize & version Size
$FileSizeinGB += [Math]::Round(($_.FieldValues.File_x0020_Size/1GB),2)
$File = Get-PnPProperty -ClientObject $_ -Property File
$Versions = Get-PnPProperty -ClientObject $File -Property Versions
If($Versions.Count -gt 0){
$VersionSize += $Versions | Measure-Object -Property Size -Sum | Select-Object -expand Sum
$VersionSizeinGB += [Math]::Round(($VersionSize/1GB),2)
}
Else{
#Write-host -f Red "No Versions Found!"
}
}
}
$TotalFileSizeKB = [Math]::Round(($FileSizeinGB + $VersionSizeinGB),2)
"$($SiteURL) `t $($FileSizeinGB) `t $($VersionSizeinGB) `t $($TotalFileSizeGB)" | Out-File $CSVPath -Append
}
foreach ($s in $sitelist)
{
#Connect to SharePoint Online Site from PnP Online
Connect-PnPOnline -Url $s.URL -Interactive
Write-host "Reporting started for "$s.URL -f Yellow
$start = Get-Date
#Get all Webs and call the function to set version history
Get-PnPSubWeb -IncludeRootWeb | ForEach-Object {Enable-PnPVersionReport $s.URL}
Write-host "Reporting complete for "$s.URL" Time Elapsed: "(New-TimeSpan -Start $start -End (Get-Date)) -f Yellow
}
$sitelist = Import-CSV C:\SWSetup\SPOSiteList.csv
$SPAdminURL = "https://contoso-admin.sharepoint.com"
Connect-PnPOnline -Url $SPAdminURL -Interactive
#Exclude certain libraries
$ExcludedLists = ("Preservation Hold Library","Site Assets","Site Pages","Style Library","Form Templates")
$datestring = (Get-Date).ToString(“s”).Replace(“:”,”-”)
$CSVPath = "C:\SWSetup\VersionSizeSummary_" + $($datestring) +".csv"
"SiteURL `t File Size (GB) `t Version Size (GB) `t Total File Size (GB)" | Out-File $CSVPath -Append
Function Enable-PnPVersionReport($SiteURL)
{
#Connect to SharePoint Online site
Connect-PnPOnline -Url $SiteURL -Interactive
$FileSizeinGB = 0
$VersionSize = 0
$VersionSizeinGB = 0
$TotalFileSizeGB = 0
$Lists = Get-PnPList | Where-Object {$_.Hidden -eq $False -and $_.BaseType -eq "DocumentLibrary" -and $ExcludedLists -notcontains $_.Title}
Foreach ($l in $Lists)
{
#Write-host "Connecting to $($l.Title)" -f Magenta
$items = Get-PnPListItem -List $($l.Title)
$VersionHistoryData = @()
#Iterate through all files
Get-PnPListItem -List $l -PageSize 500 | Where {$_.FieldValues.FileLeafRef -like "*.*" -and [string]::IsNullOrEmpty($_.FieldValues.File_x0020_Type) -ne "True" } | ForEach-Object {
#Write-host "Getting Versioning Data of the File:"$_.FieldValues.FileRef -f Green
#Get FileSize & version Size
$FileSizeinGB += [Math]::Round(($_.FieldValues.File_x0020_Size/1GB),2)
$File = Get-PnPProperty -ClientObject $_ -Property File
$Versions = Get-PnPProperty -ClientObject $File -Property Versions
If($Versions.Count -gt 0){
$VersionSize += $Versions | Measure-Object -Property Size -Sum | Select-Object -expand Sum
$VersionSizeinGB += [Math]::Round(($VersionSize/1GB),2)
}
Else{
#Write-host -f Red "No Versions Found!"
}
}
}
$TotalFileSizeKB = [Math]::Round(($FileSizeinGB + $VersionSizeinGB),2)
"$($SiteURL) `t $($FileSizeinGB) `t $($VersionSizeinGB) `t $($TotalFileSizeGB)" | Out-File $CSVPath -Append
}
foreach ($s in $sitelist)
{
#Connect to SharePoint Online Site from PnP Online
Connect-PnPOnline -Url $s.URL -Interactive
Write-host "Reporting started for "$s.URL -f Yellow
$start = Get-Date
#Get all Webs and call the function to set version history
Get-PnPSubWeb -IncludeRootWeb | ForEach-Object {Enable-PnPVersionReport $s.URL}
Write-host "Reporting complete for "$s.URL" Time Elapsed: "(New-TimeSpan -Start $start -End (Get-Date)) -f Yellow
}
The output file will look something like this:
Original Script
This is the original script that I based my script off of. It generates the version history analysis for all libraries on a single site. Additional sources below that helped me modify this script.
#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/vendors"
$CSVFile = "C:\Temp\VersionHistoryRpt.csv"
#Delete the Output report file if exists
If (Test-Path $CSVFile) { Remove-Item $CSVFile }
#Connect to SharePoint Online site
Connect-PnPOnline -Url $SiteURL -Interactive
#Get All Document Libraries from the Web - Exclude Hidden and certain lists
$ExcludedLists = @("Form Templates", "Preservation Hold Library","Site Assets", "Pages", "Site Pages", "Images",
"Site Collection Documents", "Site Collection Images","Style Library")
$Lists = Get-PnPList | Where-Object {$_.Hidden -eq $False -and $_.Title -notin $ExcludedLists -and $_.BaseType -eq "DocumentLibrary"}
#Iterate through all files from all document libraries
ForEach($List in $Lists)
{
$global:counter = 0
$Files = Get-PnPListItem -List $List -PageSize 2000 -Fields File_x0020_Size, FileRef -ScriptBlock { Param($items) $global:counter += $items.Count; Write-Progress -PercentComplete ($global:Counter / ($List.ItemCount) * 100) -Activity "Getting Files of '$($List.Title)'" -Status "Processing Files $global:Counter to $($List.ItemCount)";} | Where {$_.FileSystemObjectType -eq "File"}
$VersionHistoryData = @()
$Files | ForEach-Object {
Write-host "Getting Versioning Data of the File:"$_.FieldValues.FileRef
#Get File Size and version Size
$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 -expand Sum
$VersionSizeinKB = [Math]::Round(($VersionSize/1KB),2)
$TotalFileSizeKB = [Math]::Round(($FileSizeinKB + $VersionSizeinKB),2)
#Extract Version History data
$VersionHistoryData+=New-Object PSObject -Property ([Ordered]@{
"Library Name" = $List.Title
"File Name" = $_.FieldValues.FileLeafRef
"File URL" = $_.FieldValues.FileRef
"Versions" = $Versions.Count
"File Size (KB)" = $FileSizeinKB
"Version Size (KB)" = $VersionSizeinKB
"Total File Size (KB)" = $TotalFileSizeKB
})
}
$VersionHistoryData | Export-Csv -Path $CSVFile -NoTypeInformation -Append
}
$SiteURL = "https://crescent.sharepoint.com/sites/vendors"
$CSVFile = "C:\Temp\VersionHistoryRpt.csv"
#Delete the Output report file if exists
If (Test-Path $CSVFile) { Remove-Item $CSVFile }
#Connect to SharePoint Online site
Connect-PnPOnline -Url $SiteURL -Interactive
#Get All Document Libraries from the Web - Exclude Hidden and certain lists
$ExcludedLists = @("Form Templates", "Preservation Hold Library","Site Assets", "Pages", "Site Pages", "Images",
"Site Collection Documents", "Site Collection Images","Style Library")
$Lists = Get-PnPList | Where-Object {$_.Hidden -eq $False -and $_.Title -notin $ExcludedLists -and $_.BaseType -eq "DocumentLibrary"}
#Iterate through all files from all document libraries
ForEach($List in $Lists)
{
$global:counter = 0
$Files = Get-PnPListItem -List $List -PageSize 2000 -Fields File_x0020_Size, FileRef -ScriptBlock { Param($items) $global:counter += $items.Count; Write-Progress -PercentComplete ($global:Counter / ($List.ItemCount) * 100) -Activity "Getting Files of '$($List.Title)'" -Status "Processing Files $global:Counter to $($List.ItemCount)";} | Where {$_.FileSystemObjectType -eq "File"}
$Files | ForEach-Object {
Write-host "Getting Versioning Data of the File:"$_.FieldValues.FileRef
#Get File Size and version Size
$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 -expand Sum
$VersionSizeinKB = [Math]::Round(($VersionSize/1KB),2)
$TotalFileSizeKB = [Math]::Round(($FileSizeinKB + $VersionSizeinKB),2)
$VersionHistoryData+=New-Object PSObject -Property ([Ordered]@{
"Library Name" = $List.Title
"File Name" = $_.FieldValues.FileLeafRef
"File URL" = $_.FieldValues.FileRef
"Versions" = $Versions.Count
"File Size (KB)" = $FileSizeinKB
"Version Size (KB)" = $VersionSizeinKB
"Total File Size (KB)" = $TotalFileSizeKB
})
}
$VersionHistoryData | Export-Csv -Path $CSVFile -NoTypeInformation -Append
}
Sources:
- SharePoint Online: Version History Report using PowerShell - SharePoint Diary
- Check if a String Is Not NULL or EMPTY in PowerShell | Delft Stack
- SharePoint Online: PowerShell to Get Managed Metadata Field Value - SharePoint Diary
- How to Use PowerShell Where-Object to Filter All the Things (adamtheautomator.com)
- How Do I get the version of a file using PnP Powershell - Microsoft Community Hub
- SharePoint PnP List Item -Show All Fields – My Broken SharePoint (wordpress.com)
- SharePoint Online: Download All Versions of a Document using PowerShell - SharePoint Diary
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.