Powershell: SharePoint 2007 vs 2010
July 27, 2011 1 Comment
It still amazes me the differences between writing powershell for SharePoint 2007 and SharePoint 2010. For example, look at the differences when you try to find the number of spwebs within a farm that have a template “STS#1”:
2010:
Add-PSSnapin “Microsoft.SharePoint.Powershell”
(Get-SPWebApplication -IncludeCentralAdministration | Get-SPSite -Limit all| Get-SPWeb -Limit all | where{$_.WebTemplate -eq “STS” -and $_.WebTemplateId -eq “1”}).count
2007:
[System.Reflection.Assembly]::LoadWithPartialName(”Microsoft.SharePoint”)
$total = 0
$farm = [Microsoft.SharePoint.Administration.SPFarm]::Local
foreach($spService in $farm.Services) {
if($spService -is [Microsoft.SharePoint.Administration.SPWebService]) {
foreach($webapp in $spService.WebApplications) {
foreach($site in $webapp.Sites) {
foreach($web in $site.AllWebs) {
if($web.WebTemplate -eq “STS” -and $web.WebTemplateId -eq “1”) {
$total +=1
}
}
}
}
}
}
Write-host $total
Interesting post! I think in 2007 you could also do a stsadm -o enumwebs and parse the xml if that was easier to you. That script would work as well.