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
An oft overlooked portion of migration prep… thanks for this Henry!
Great script! But noticed a tiny spelling mistake, it should be
foreach($user in $web.SiteAdministrators)
instead of
foreach($user in $web.SiteAdministrator)
Thanks Rene! Post updated to reflect change.
Excellent Script. Thanks for sharing…
I was almost shocked to find that someone had created such a script. It appears to offer just the info I need. I’m wondering if anyone received an error regarding a missing “)” in method call?
The missing ‘)’ error is due to a copy/paste problem with line 58, the character after 758 got corrupted when I copied the script off the HTML, I think it should read:
if($templateID.startswith(“758”))
Thanks a lot.. this was really helpful…!
I copied the example above into Notepad to save as a script, but get a “missing “)” in method” error around siteadministrators. Thoughts? I’m sure I’m just making a basic mistake.
Hi there! This article could not be written any better! Looking at this post
reminds me of my previous roommate! He continually kept preaching about this.
I’ll forward this post to him. Pretty sure he will have a very good read.
I appreciate you for sharing!