0

I am wondering if someone can help with a PowerShell script to retrieve the size of Preservation hold libraries in all the SharePoint Sites and OneDrive. I need to calculate the total space being used by items in Preservation Hold Libraries in our tenant. Thanks so much for the help in advance.

Salzee
  • 1
  • 1

1 Answers1

0

You can use the following script to calculate all the file size hold in a library. Remember you need to be site collection administrator to access the Preservation Hold library.

#Set Variables
$SiteURL = "<site url>"
$LibraryName = "Documents"
  
#Connect to SharePoint Online site
Connect-PnPOnline -Url $SiteURL -UseWebLogin
 
$FileData = @()
#Iterate through all files
Get-PnPListItem -List $LibraryName -PageSize 500 | Where {$_.FieldValues.FileLeafRef -like "*.*"} | ForEach-Object {
    Write-host "Getting Size of the File:"$_.FieldValues.FileRef -NoNewline
    #Get FileSize & 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)
    Write-host `t $TotalFileSizeKB "KB" -f Yellow
 
    #extract File Size data
    $FileData+=New-Object PSObject -Property  ([Ordered]@{
        "File Name"  = $_.FieldValues.FileLeafRef
        "File URL" = $_.FieldValues.FileRef
        "File Size (KB)"  = $FileSizeinKB
        "Version Size (KB)"   = $VersionSizeinKB
        "Total File Size (KB)" = $TotalFileSizeKB
    })
}
$FileData | Format-table
#Calculate the Total Size of the document library
$LibrarySize = [Math]::Round((($FileData | Measure-Object -Property "Total File Size (KB)" -Sum | Select-Object -expand Sum)/1KB),2)
Write-host -f Green "Total Library Size (MB):" $LibrarySize

Reference: https://www.sharepointdiary.com/2018/06/sharepoint-online-get-list-library-size-using-powershell.html

Jerry_MSFT
  • 276
  • 1
  • 3
  • On the line:$VersionSize = $Versions | Measure-Object -Property Size -Sum | Select-Object -expand Sum I get an error "An error occurred while enumerating through a collection: The collection has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.." Any advice? – Ofer Gal Jul 06 '21 at 23:38