SharePoint Online: Get File Size using PowerShell
Requirement: Get file size in SharePoint Online using PowerShell.
How to Get the File Size in SharePoint Online?
Have you ever needed to get the size of a file in SharePoint Online? Maybe you need to know how much storage space is occupied by a file in your SharePoint Online document library. In this blog post, we’ll show you how to use PowerShell to get the size of a file or folder. Also, we’ll explore getting file size information in a few simple steps. Let’s get started!
To get the size of a file, you can follow these approaches:
- Option 1: Edit the list view of the document library and include the “File Size” column in it
- Option 2: Go to the “Storage Metrics” page, and you can get the size of the files.
- Option 3: You can open the library in explorer view and get the file size from the file’s properties.
- Option 4: You can open the site in SharePoint Designer to get the size of the files.
PowerShell to Get File Size in SharePoint Online:
To get file size in SharePoint Online, use this PowerShell script
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
Function Get-SPOFileSize($SiteURL,$FileRelativeURL)
{
#Setup Credentials to connect
$Cred = Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
Try {
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials
#Get the File
$File = $Ctx.Web.GetFileByServerRelativeUrl($FileRelativeURL)
$Ctx.Load($File)
$Ctx.ExecuteQuery()
$FileSize = [Math]::Round($File.Length/1KB,2)
#Get File Size
Write-host "File Size (KB):" $FileSize
}
Catch {
write-host -f Red "Error:" $_.Exception.Message
}
}
#Call the Function to get file size with Site URL and File URL
Get-SPOFileSize -SiteURL "https://crescent.sharepoint.com/sites/marketing" -FileRelativeURL "/sites/marketing/Branding/Technical Design.docx"
To get the total size of a file with its versions, use:
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$FileRelativeURL = "/sites/marketing/Branding/Technical Design.docx"
#Setup Credentials to connect
$Cred = Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
Try {
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials
#Get the File
$File = $Ctx.Web.GetFileByServerRelativeUrl($FileRelativeURL)
$Ctx.Load($File)
$Ctx.Load($File.ListItemAllFields)
$Ctx.ExecuteQuery()
$TotalFileSize = [Math]::Round($File.ListItemAllFields.FieldValues.SMTotalSize.LookupId/1KB,2)
#Get File Size
Write-host "Total File Size (KB):" $TotalFileSize
}
Catch {
write-host -f Red "Error:" $_.Exception.Message
}
SharePoint Online: Get File Size using PnP PowerShell
To get the File size using PnP PowerShell,
#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$FileRelativeURL = "/sites/marketing/Branding/Technical Design.docx"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
#Get the File as List Item
$File = Get-PnPFile -Url $FileRelativeURL -AsListItem
#Get File Size
Write-host "File Size:"$File["File_x0020_Size"]
If you want to analyze file size for all documents, use the below PowerShell script.
Get File Size for all documents in a SharePoint Online Document Library
Let’s get the size of each file in a document library and export the data to a CSV file using PowerShell:
#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/Marketing"
$ListName= "Branding"
$ReportOutput = "C:\Temp\FileSizeRpt.csv"
#Connect to SharePoint Online site
Connect-PnPOnline $SiteURL -Interactive
#Array to store results
$Results = @()
#Get all Items from the document library
$List = Get-PnPList -Identity $ListName
$ListItems = Get-PnPListItem -List $ListName -PageSize 500 | Where {$_.FileSystemObjectType -eq "File"}
Write-host "Total Number of Items in the List:"$List.ItemCount
$ItemCounter = 0
#Iterate through each item
Foreach ($Item in $ListItems)
{
$Results += New-Object PSObject -Property ([ordered]@{
FileName = $Item.FieldValues.FileLeafRef
RelativeURL = $Item.FieldValues.FileRef
FileSize = $Item.FieldValues.File_x0020_Size
TotalFileSize = $Item.FieldValues.SMTotalSize.LookupId
})
$ItemCounter++
Write-Progress -PercentComplete ($ItemCounter / ($List.ItemCount) * 100) -Activity "Processing Items $ItemCounter of $($List.ItemCount)" -Status "Getting data from Item '$($Item['FileLeafRef'])"
}
#Export the results to CSV
$Results | Export-Csv -Path $ReportOutput -NoTypeInformation
Write-host "File Size Report Exported to CSV Successfully!"
Here is another post on adding a file size column to SharePoint Online document library list view: How to add file size column in SharePoint Online?
In summary, getting the file size of a specific file or multiple files in SharePoint Online can be easily accomplished using PowerShell. Additionally, you can also export this information to a CSV file for further analysis and reporting.
What the the format of the file size when exported to a csv? Comparing what is listed in sharepoint vs the output of the script don’t seem to match.