SharePoint Online: Get Document Library Size using PowerShell

Requirement: Get the Size of a SharePoint Online Document Library
Get SharePoint Online Document Library Size using PowerShell

PowerShell to Get List or Document Library Size in SharePoint Online:
#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 to Get the size of a List or Library in SharePoint Online
Function Get-SPOListSize($SiteURL, $ListName)
{ 
     Try
     {
        #Get credentials to connect to SharePoint Online
        $Credentials = Get-Credential

        #Set up the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
    
        #Query to Get all List Items from all folders - exclude Folder objects
        $Query = New-Object Microsoft.SharePoint.Client.CamlQuery
        $Query.ViewXml = "@
        <View Scope='RecursiveAll'>  
            <Query> 
               <Where>
                     <Eq>
                           <FieldRef Name='FSObjType' /><Value Type='Integer'>0</Value>
                     </Eq>
               </Where> 
            </Query> 
        </View>"

        #Get All Items from the List
        $List = $Ctx.Web.Lists.GetByTitle($ListName)
        $ListItems = $List.GetItems($Query) 
        $Ctx.Load($ListItems)
        $Ctx.ExecuteQuery()
        
        $i=1;$TotalSize = 0
        ForEach($ListItem in $ListItems)
        {
            Write-host -f Yellow "Calculating Size of Item $i of $($ListItems.count)..."
            #Get the File
            $File = $ListItem.File
            $Ctx.Load($File)
            $Ctx.Load($File.Versions)
            $Ctx.ExecuteQuery()

            $FileSize =0; $VersionSize = 0
            If($File.Versions.Count -ge 1)
            {
                $VersionSize = $File.Versions | Measure-Object -Property Size -Sum | Select-Object -expand Sum
            }
            $FileSize =  $ListItem.File.Length  + $VersionSize
            Write-host "Total Size of the File '$($File.Name)' : $FileSize"

            $TotalSize += $FileSize
            $i++
            #Clear
        }
        Return [Math]::Round($TotalSize/1MB, 2)
    }
    Catch [System.Exception]
    {
        Write-Host -f Red "Error:"$_.Exception.Message
    }
}
  
#parameters
$SiteURL = "https://crescenttech.sharepoint.com"
$ListName="Documents"
  
#Call the function to get Subsite size
$ListSize = Get-SPOListSize -SiteURL $SiteURL -ListName $ListName
Write-host -f Green "`nToal Size of the List: $ListSize MB"

PnP PowerShell to Get Document Library Size
#Set Variables
$SiteURL = "https://crescent.sharepoint.com/sites/Marketing"
$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
Scriput output:
powershell to get document library size in sharepoint online

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.