PowerShell Example how to iterate through all Sites and Webs

This code came from here. It will output the result to CSV file in the current directory where you rrun this script

$site = get-spsite
$sitecount = $site.count
$date = get-date -uformat “%Y%m%d”
$filename = “sitetemplate_$date.csv”
“Enumerating List of sites and subwebs” | Out-File -FilePath $filename
for ($i=0;$i -lt $sitecount;$i++)
{
$webinthisite = $site[$i].AllWebs
$webscount = $webinthisite.count
#”$i $site[$i].Url $webscount subwebs” | Out-File -FilePath $filename -Append
“SiteURL – SiteTemplateName – SiteTemplateId” | Out-File -FilePath $filename -Append
for ($j=0;$j -lt $webscount;$j++)
{
$siteTitle = $webinthisite[$j]
$siteUrl = $webinthisite[$j].Url
$siteTemplate = $webinthisite[$j].WebTemplate
$siteTemplateID = $webinthisite[$j].WebTemplateID
$str = “$siteUrl – $siteTitle – $siteTemplate – $siteTemplateID” | Out-File -FilePath $filename -Append
}
“===================================================” | Out-File -FilePath $filename -Append
$webinthissite.dispose
}

Advertisements