SharePoint Online: Get Subsite Size using PowerShell
Requirement: Get Subsite Size in SharePoint Online.
How to Get the Subsite Size in SharePoint Online?
Are you looking for a way to get the size of your subsites in SharePoint Online? Well, there are a few approaches you can take, and we’ll walk through each one to get the size of any subsite in SharePoint Online! In this post, I will show you how to get subsite size information in SharePoint Online from the web user interface. Also, I will share the script to find the subsite size in SharePoint Online using PowerShell.
To find out the storage quota occupied by a subsite in SharePoint Online, go to:
- Navigate to your SharePoint Online subsite >> Click on Settings gear >> Site Information >> View all site settings.
- In the Site Settings page, click on the “Storage Metrics” link under “Site Collection Administration”.
This page gets you the size of each subsite from the particular site.
Get Subsite Size in SharePoint Online using PowerShell
As a SharePoint Online administrator, you can use PowerShell to get the size of a site collection or subsite. This can be useful when trying to identify which site collections or subsites are using the most storage space. Here is the PowerShell to get the 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://Crescent.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 the storage consumption of all subsites in a site collection?
PowerShell to Find the Size of All Subsites in a SharePoint Online Site Collection
Let’s get the size of all subsites in a site collection and export them to a 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://Crescent.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
This script will return a list of subsites and the amount of storage that is currently being used. If you want to get the site collection size in SharePoint Online using PowerShell, use: Check SharePoint Online Site Collection Storage Size using PowerShell
Hello mate, thanks a lot for sharing this knowledge! It helped me a lot.
Could you please share a PS script to count the total size of only the final versions of files under a site collection or subsite?
Thanks a lot in advance!
The “$File.Length” gets you the size of the file without previous versions! So, the last script is what you are looking for!
Hi, How can you get the count of subsites in a site collection?
Use this script:
#Connect to PnP Online
Connect-PnPOnline -Url “https://crescent.sharepoint.com/sites/marketing” -Interactive
#Get Subsites count
(Get-PnPSubWeb -Recurse).count