This script opens an SPWebApplication object for the given url, retrieves the Site Collections, and counts the webs in each.
$webappUrl = "http://localhost" 
$webapp = Get-SPWebApplication $webappUrl 
$allWebsCount = 0 
$webapp.Sites | % { $allWebsCount +$_.AllWebs.Count; $_.Dispose(); } 
Write-Host "There are" $allWebsCount "webs" -ForegroundColor Green
The script assumes the following:
  • you are either using the SharePoint Management Shell or have imported the Microsoft.SharePoint.PowerShell snapin
  • you are running on the SharePoint Server
  • you have administrative access to the web application, site collection, and webs