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.
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? This blog post will show you how to find the number of subsites in a SharePoint Online site collection.
#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?
#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