SharePoint Online: Get the Number of Subsites in Each Site Collection
Requirement: Find out the number of subsites in each site collection in SharePoint Online.
How to Get the Number of Subsites in SharePoint Online Site Collection?
To get the subsites in a SharePoint Online site collection, do the following:
- Login to your SharePoint Site >> Click on Settings >> Choose “Site Contents”
- On the site contents page, click on “Subsites” to get all the subsites under the current site.
However, this method gets only the top-level subsites (Subsites under the root site collection). So, let’s use PowerShell to get all subsites in a site.
PowerShell to Find the Total Number of Subsites in a SharePoint Online Site Collection
How do you find out how many subsites are in a site collection? You can use the Get-SPOSite cmdlet to get the site collection and then use the WebsCount property to get the total number of subsites. Here’s an example:
#Parameters
$AdminCenterURL = "https://Crescent-admin.sharepoint.com"
$SiteURL = "https://Crescent.sharepoint.com/sites/Marketing"
#Connect to SharePoint Online
Connect-SPOService -Url $AdminCenterURL -Credential (Get-Credential)
#Get the Site
$Site = Get-SPOSite -Identity $SiteURL
#Get Total number of subsites
Write-host "Total Number of Subsites:" ($Site.WebsCount-1)
The “WebsCount” property gets the total number of webs on the site, including the RootWeb. How about getting the total number of subsites for all sites in SharePoint Online? To get the number of subsites in all site collections in a SharePoint Online tenant, you can use PowerShell to loop through each site collection and get the total number of subsites.
#Parameters
$AdminCenterURL = "https://crescent-admin.sharepoint.com"
$SitesData = @()
#Connect
Connect-SPOService -Url $AdminCenterURL -Credential (Get-Credential)
#Get All Site Collections
$Sites = Get-SPOSite -Limit All
#Loop through each site
ForEach($Site in $Sites)
{
#Get the Site
$Site = Get-SPOSite -Identity $Site
#Collect subsites data
$SitesData += New-Object PSObject -property $([ordered]@{
SiteName = $Site.Title
SiteURL = $Site.Url
Subsites = $Site.WebsCount-1
})
}
$SitesData
This script gets you the subsites count on all levels of each site collection in the tenant.
PnP PowerShell to Get Subsites Count on Each Site collection
This time, let’s extract the subsites count for all site collections and export the findings to a CSV.
#Parameters
$SiteURL ="https://crescent-admin.sharepoint.com"
$CSVFile = "C:\Temp\SubsitesCount.csv"
$SitesData = @()
#Connect to PnP Online
Connect-PnPOnline -URL $SiteURL -Interactive
#Get All Site Collections
$SiteCollections = Get-PnPTenantSite
#Loop through each site
ForEach($SiteColl in $SiteCollections)
{
#Get the Site
$Site = Get-PnPTenantSite -Identity $SiteColl
#Collect Data
$SitesData += New-Object PSObject -property $([ordered]@{
SiteName = $Site.Title
SiteURL = $Site.Url
Subsites = $Site.WebsCount-1
})
}
$SitesData
$SitesData | Export-CSV $CSVFile -NoTypeInformation
If you want to get more details on sites and subsites, refer: SharePoint Online: PowerShell to Get All Site Collections and Subsites