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?
To get the size of a file, you can follow these approaches:
- Edit the list view of the document library and include "File Size" column to it
- Go to "Storage Metrics" page and you can get size of the files.
- You can open the library in explorer view and get the file size from file's properties.
- You can open the site in SharePoint Designer to get 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 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 -UseWebLogin #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 size of the 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 -UseWebLogin #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 my another post on adding file size column to SharePoint Online document library list view: How to add file size column in SharePoint Online?
No comments:
Please Login and comment to get your questions answered!