Hey everyone! I’ve been knee deep in a migration project the last month and just now got a breather to post some of the goodies I’ve come up with. So in analyzing our current SharePoint farm we wanted to generate a report of what kind of SharePoint sites exist in our environment. We were looking for these attributes:

– When was the site created?
– When was the site last modified?
– Who owns the site?
– Site name
– Site URL
– The name of the template being used
– The template ID
– Whether or not it’s a Fab40 site
– How many items are in the site?
– Whether or not the site is empty

How did we use this script?

I originally wrote this script to detect all instances of Fab40 sites so that we could deal with them (get rid of them prior to upgrading to SharePoint 2010), but then extended it to report on all site templates. This gave me the information I needed to reach out to all the site owners to clean up empty and/or low usage sites (we don’t have automatic site deletion turned on).

The Script

# This script will generate a report of all sites in the web app
# along with how many items are within the site.
#
# Author: Henry Ong

######################## Start Variables ########################
$siteURL = “http://siteurl” #URL to any site in the web application.
$filePath = “D:\PowerShellScripts\AllSites.csv”
######################## End Variables ########################

if(Test-Path $filePath)
{
Remove-Item $filePath
}
Clear-Host

[System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint”)

# Creates an object that represents a SharePoint site.
function CreateNewObject
{
$customObject = New-Object system.Object
$customObject | Add-Member -type NoteProperty -Name WebCreated -Value $web.Created.ToShortDateString()
$customObject | Add-Member -type NoteProperty -Name WebModified -Value $web.LastItemModifiedDate.ToShortDateString()
$customObject | Add-Member -type NoteProperty -Name RequestAccessEmail -Value $web.RequestAccessEmail
$customObject | Add-Member -type NoteProperty -Name WebTitle -Value $web.Title
$customObject | Add-Member -type NoteProperty -Name WebURL -Value $web.URL
$customObject | Add-Member -type NoteProperty -Name WebTemplateName -Value $web.WebTemplate
$customObject | Add-Member -type NoteProperty -Name WebTemplateID -Value $web.WebTemplateId
$customObject | Add-Member -type NoteProperty -Name Fab40Site -Value $false
$customObject | Add-Member -type NoteProperty -Name ItemCount -Value “”
$customObject | Add-Member -type NoteProperty -Name SiteAdmins -Value “”
$customObject | Add-Member -type NoteProperty -Name GreaterThanZeroItems -Value $false

return $customObject
}

$site = new-object microsoft.sharepoint.spsite($siteURL)
$webApp = $site.webapplication
$allSites = $webApp.sites

$customObjectsList =@()

foreach

($site in $allSites)
{
$allWebs = $site.AllWebs

foreach ($web in $allWebs)
{
$itemCount = 0;

$customWebObject = CreateNewObject

$templateID = $web.WebTemplateID.ToString()

if($templateID.startswith(“758”))
{
$customWebObject.Fab40Site = $true
}

foreach ($list in $web.Lists)
{
if(($list.ItemCount -gt 0) -and ($list.Hidden -ne $true))
{
$customWebObject.GreaterThanZeroItems = $true

$itemCount += $list.ItemCount;
}
}

$customWebObject.ItemCount = $itemCount.ToString()
$customObjectsList += $customWebObject

foreach($user in $web.SiteAdministrators)
{
$customWebObject.SiteAdmins += “$user ”
}

Write-Host $web.title
$web.Dispose()
}
$site.dispose()
}

# Exporting the data to a CSV file
$customObjectsList | Select-Object WebCreated,WebModified,WebTitle,WebURL,WebTemplateName,WebTemplateID,Fab40Site,ItemCount,GreaterThanZeroItems,SiteAdmins,RequestAccessEmail | Export-Csv $filePath
write-host “Done”

 Don’t forget to use PowerGUI! PowerGUI is your friend – http://powergui.org/index.jspa