简体   繁体   中英

PowerShell for retrieving SharePoint Online Document Library Size

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM