SharePoint Online: Get Document Library Size using PowerShell

Requirement: Get the Size of a SharePoint Online Document Library

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"
SharePoint Online: Get Document Library Size using PowerShell SharePoint Online: Get Document Library Size using PowerShell Reviewed by Salaudeen Rajack on June 10, 2018 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.