
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:
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