SharePoint Online: Get Subsite Size using PowerShell

Requirement: Get Subsite Size in SharePoint Online

How to Get Subsite Size in SharePoint Online?
To find out the storage quota occupied by a subsite in SharePoint Online, go to:
  • Site Settings >> Click on "Storage Metrics" link under "Site Collection Administration"
    sharepoint online subsite size
    Please note the data shown in storage metrics is not real time! It takes a while for a background timer job to run and populate this data.

Get Subsite Size in SharePoint Online using PowerShell
Here is the PowerShell to get subsite 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 Folder in SharePoint Online
Function Get-SPOFolderSize([Microsoft.SharePoint.Client.Folder]$Folder)
{ 
     Try
     {
        Write-host -f Yellow "Size of the Folder '$($Folder.ServerRelativeUrl)' : " -NoNewline
        
        $FolderSize = 0
        #Get all Files and Subfolders from the folder
        $Ctx.Load($Folder.Files)
        $Ctx.Load($Folder.Folders)
        $Ctx.ExecuteQuery()

        ForEach($File in $Folder.Files)
        {
            #Get File Size
            $FolderSize += $File.Length
        }
        Write-host $FolderSize

        #Process all Sub Folders
        ForEach($Folder in $Folder.Folders)
        {
            #Call the function recursively
            $FolderSize +=Get-SPOFolderSize $Folder
        }
        Return $FolderSize
    }
    Catch [System.Exception]
    {
        Write-Host -f Red "Error:"$_.Exception.Message
    }
}
 
#parameters
$SiteURL = "https://crescenttech.sharepoint.com/unitedstates"
 
#Get credentials to connect to SharePoint Online Admin Center
$Cred = 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)
   
#Get the Web
$Web = $Ctx.Web
$RootFolder = $Web.RootFolder
$Ctx.Load($RootFolder)
$Ctx.ExecuteQuery()

#Call the function to get Subsite size
$SubSiteSize = Get-SPOFolderSize -Folder $RootFolder

#sharepoint online powershell site size
Write-host "Toal Size of the subsite (MB): " -NoNewline
[Math]::Round($SubSiteSize/1MB, 2)
This PowerShell script gets the size of a given subsite. How about retrieving storage consumption of all subsites in a site collection?

PowerShell to Find Size of All Subsites in a SharePoint Online Site Collection
Lets get the size of all subsites in a site collection and export to CSV file.
#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 Folder in SharePoint Online
Function Get-SPOFolderSize([Microsoft.SharePoint.Client.Folder]$Folder)
{ 
     Try
     {
        Write-host -f Cyan "`t Size of the Folder '$($Folder.ServerRelativeUrl)' : " -NoNewline
        
        $FolderSize = 0
        #Get all Files and Subfolders from the folder
        $Ctx.Load($Folder.Files)
        $Ctx.Load($Folder.Folders)
        $Ctx.ExecuteQuery()

        ForEach($File in $Folder.Files)
        {
            #Get File Size
            $FolderSize += $File.Length
        }
        Write-host $FolderSize

        #Process all Sub Folders
        Foreach($Folder in $Folder.Folders)
        {
            #Call the function recursively
            $FolderSize +=Get-SPOFolderSize $Folder
        }
        Return $FolderSize
    }
    Catch [System.Exception]
    {
        Write-Host -f Red "Error:"$_.Exception.Message
    }
}

Function Get-SPOSubsiteSize([Microsoft.SharePoint.Client.Web]$Web)
{  
    Write-host -f Yellow "Finding the Size of the subsite:"$web.Url 
    $StorageMetrics= @()

    #Get the Root Folder of the Web
    $Ctx = $Web.Context
    $RootFolder = $Web.RootFolder
    $Ctx.Load($RootFolder)
    $Ctx.Load($Web.Webs)
    $Ctx.ExecuteQuery()

    #Call the function to get Subsite size
    $SubSiteSize = Get-SPOFolderSize -Folder $RootFolder
    $SizeinMB = [Math]::Round($SubSiteSize/1MB, 2)
    Write-host -f Green "Toal Size of the subsite (MB): $SizeinMB `n"
    
    #Add the result to an Array
    $StorageData = New-Object PSObject
    $StorageData | Add-Member NoteProperty SiteURL($web.Url)
    $StorageData | Add-Member NoteProperty Size-MB([math]::Round($SubSiteSize/1MB,2))
    $StorageMetrics += $StorageData

    #Process all subsites
    ForEach($Subweb in $web.Webs)
    {         
        $StorageMetrics+=Get-SPOSubsiteSize $Subweb
    }
    Return $StorageMetrics
}

#parameters
$SiteURL = "https://crescenttech.sharepoint.com"
$CSVPath="C:\Temp\SubsiteStorage.csv"

#Delete the Output Report, if exists
If (Test-Path $CSVPath) { Remove-Item $CSVPath }

#Get credentials to connect to SharePoint Online Admin Center
$Cred = 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)
   
#Get the Root Web
$Web = $Ctx.Web
$Ctx.Load($Web)
$Ctx.ExecuteQuery()

#Call the function to get subsite storage metrics
$SubsiteStorageMetrics = Get-SPOSubsiteSize $Web

#Export the results to CSV
$SubsiteStorageMetrics | Export-CSV -LiteralPath $CSVPath -NoTypeInformation
If you want to get the site collection size in SharePoint Online using PowerShell, use: Check SharePoint Online Site Collection Storage Size using PowerShell
SharePoint Online: Get Subsite Size using PowerShell SharePoint Online: Get Subsite 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.