I want to export size of document library for whole tenant

JaixuSanzhi 25 Reputation points
2023-06-30T03:11:04.4+00:00

I want to get all document library size from tenant

for this i follow the script below

it will only show size for one document library

so I though i can modify from csv i tried but not able to get all tenant library size

Can anyone help me for this

How can i modify csv so i can get all size of document library

and i want to modify site url in csv too

Set Variables
$SiteURL = "https://crescent.sharepoint.com/sites/Marketing"
$LibraryName = "Documents"
  
#Connect to SharePoint Online site
Connect-PnPOnline -Url $SiteURL -Interactive
 
$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


#Read more: https://www.sharepointdiary.com/2018/06/sharepoint-online-get-list-library-size-using-powershell.html#ixzz865EOyXN0
Microsoft 365 and Office | SharePoint Server | For business
Microsoft 365 and Office | SharePoint | For business | Windows
Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Anonymous
    2023-07-03T06:32:42.6566667+00:00

    Hi @JaixuSanzhi

    Do you want to get information about files in all document libraries of a tenant?

    Here is a test for your reference:

    Here is code

    # Set Variables
    $SiteURL = "https://yoursite"
    $OutputCSVPath = "C:\Users\spsetup\Downloads\test\Output.csv"
    
    # Connect to SharePoint Online site
    Connect-PnPOnline -Url $SiteURL 
    
    # Get all document libraries in the site
    $DocumentLibraries = Get-PnPList -BaseTemplate 101 -Include Title,ParentWebUrl
    # Get all document libraries
    $DocumentLibraries = Get-PnPList | Where-Object {$_.BaseType -eq "DocumentLibrary"}
    
    foreach ($Library in $DocumentLibraries) {
        $LibraryName = $Library.Title
        $LibraryURL = $Library.ParentWebUrl + "/" + $LibraryName
    
        # Get all files in the document library
        $Items = Get-PnPListItem -List $LibraryName -PageSize 500 | Where-Object {$_.FileSystemObjectType -eq "File"}
    
        foreach ($Item in $Items) {
            Write-Host "Getting Size of the File:" $Item.FieldValues.FileRef -NoNewline
    
            # get file size
            $FileSizeinBytes = $Item.FieldValues.File_x0020_Size
            $FileSizeinKB = [Math]::Round(($FileSizeinBytes / 1KB), 2)
            Write-Host "`t" $FileSizeinKB "KB" -ForegroundColor Yellow
    
            # extract file size data
            $FileData += New-Object PSObject -Property ([Ordered]@{
                "Library Name"         = $LibraryName
                "Library URL"          = $LibraryURL
                "File Name"            = $Item.FieldValues.FileLeafRef
                "File URL"             = $Item.FieldValues.FileRef
                "File Size (KB)"       = $FileSizeinKB
            })
        }
    }
    # Export data to CSV
    $FileData | Export-Csv -Path $OutputCSVPath -NoTypeInformation
    
    # Calculate the total size of all document libraries
    $LibrarySize = [Math]::Round(($FileData | Measure-Object -Property "File Size (KB)" -Sum).Sum, 2)
    Write-Host -ForegroundColor Green "Total Library Size (KB):" $LibrarySize
    
    
    
    

    Here is test result:

    User's image


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best Regards

    Cheng Feng


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.