Sharepoint - No. of subsites in a site collection?

or simply:

$site = Get-SPSite http://YourSharePointSite
$site.AllWebs.Count

if ($ver.Version.Major -gt 1) {$host.Runspace.ThreadOptions = "ReuseThread"} 
if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) 
{
    Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}

$sites = Get-SPSite <SiteCollectionUrl>   
$noOfWebs = 0

foreach ($web in $sites.AllWebs)
{
    $noOfWebs++
}

If ($sites -ne $null)
{
    $sites.Dispose();
}

If ($web -ne $null)
{
    $web.Dispose();
}

Write-Host "Total No of Subsites: " $noOfWebs

Using the following code, within the SharePoint Management Shell, you can get the count of Web Applications in the Farm, Site Collections in Web Application, Sites in Site Collection.

Write-Host "Web Applications in Farm"
$WebApplications = Get-SPWebApplication
$WebApplications | Measure-Object | Format-List Count

ForEach($WebApplication in $WebApplications) {
    Write-Host "Site Collections in the Web Application named " $WebApplication.Name
    $WebApplication.Sites | Measure-Object | Format-List Count

    ForEach($SiteCollection in $WebApplication.Sites) {
        Write-Host "Webs (Sub-Sites) in the Site Collection with the URL " $SiteCollection.Url
        Write-Host "`nCount : " $SiteCollection.AllWebs.Count "`n`n`n"    
    }    
}

Tags:

Powershell