In the last post I used a script to create a site directory of all sites. Now what if you don’t want all sites. What if you want to create a directory of all blog sub sites.

Add-PsSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

$SPAssignment = Start-SPAssignment

#moddify these variables for the directory location
$URL = ""
$title = "Site List"
$description = "This is a list of all sites that have been created."

#delete the previous list
$SPWeb = Get-SPWeb $URL
$list = $SPWeb.Lists[$title]
$list.AllowDeletion = $true

#create list
$SPTemplate = $SPWeb.ListTemplates["Custom List"]

#add fields to list
$spFieldType = [Microsoft.SharePoint.SPFieldType]::Text
$list = (Get-SPWeb -identity $URL).Lists[$Title]
$List.Fields.Add("Secondary Contact",$spFieldType,$false)

#create default view with added columns
$listURL = $URL + "/Lists/Site%20List/AllItems.aspx"
$SPWeb = Get-SPWeb $URL
$spView = $SPWeb.GetViewFromUrl($listURL)
$spView.ViewFields.Add("Secondary Contact")

foreach ($web in get-spsite -limit all | where {$_ -notlike "*Office_Viewing_Service_Cache"}  | Get-SPWeb -limit all  | Where {$_.WebTemplate -eq "BLOG"}) {
    $site = Get-SPSite $
	$newItem = $list.Items.Add()
	$newItem["Title"] = $web.URL
    $newItem["Name"] = $web.Name
	$newItem["URL"] = $web.Url
    $newItem["Owner"] = $site.Owner.displayname
    $newItem["Secondary Contact"] = $site.SecondaryContact.DisplayName

Stop-SPAssignment $SPAssignment

This script is essentially the same. I did make a few changes in the fields and the view. The most notable change is in the get-spsite cmdlet in the foreach loop.

get-spsite -limit all | where {$_ -notlike "*Office_Viewing_Service_Cache"}  | Get-SPWeb -limit all  | Where {$_.WebTemplate -eq "BLOG"}

The first where that exludes sites like “*Office_Viewing_Service_Cache” just gets rid of that site collection which is something we don’t want list in a site directory. Then it returns all sub webs from the collection and then filters by webs created with the blog site template.

Again this script will destroy the existing list during each run. That works fine if all you want is a site directory. But what if you want to track other information thats not retrievable using the SharePoint object model? For example if you need to manually update data in an additional column thats not set by the script. I’ll go into that in the next post.