SharePoint Online: Version History Summary Report using PowerShell

 

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

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
}




Sources:
Share on Google Plus

About Tom DeMeulenaere

Highly accomplished information technology professional with extensive knowledge in System Center Configuration Manager, Windows Server, SharePoint, and Office 365.
    Blogger Comment

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.