In very large SharePoint environments, there comes a time when you need to figure out just exactly what site collections and webs you have for a given Web Application. SharePoint in nature is so easy to use that it can very quickly grow into this monster of sites collections and webs if you let the end users create their own workspaces. The following PowerShell script loops through all site collections and webs for a given Web Application and produces an HTML report file listing all entries in a hierarchical fashion.
Add-PSSnapin Microsoft.SharePoint.Powershell
function GetWebs($url)
{
$spWeb = Get-SPWeb $url
foreach($web in $spWeb.Webs)
{
$curLine = “<li><strong>{Web}</strong>” + $web.Title + ” [<em><a href='” + $web.Url + “‘>” + $web.Url + “</a></em>]</li>”
$curLine | Out-File $outFile -Append
$curLine = “<ul>”
$curLine | Out-File $outFile -Append
GetWebs($web.Url);
$curLine = “</ul>”
$curLine | Out-File $outFile -Append
$web.Dispose()
}
}
$outFile = “C:\Report.html”
$webAppUrl = Read-Host “URL of the Web Application”
$webApp = Get-SPWebApplication $webAppUrl
$curLine = “<html><head><title>” + $webAppUrl + “</title></head><body>”
$curLine | Out-File $outFile
foreach($spSite in $webApp.Sites)
{
$curLine = “<li><strong>{SC}</strong>” + $spSite.RootWeb.Title +” [<em><a href='” + $spSite.RootWeb.Url + “‘>” + $spSite.RootWeb.Url + “</a></em>]</li><ul>”
$curLine | Out-File $outFile -Append
GetWebs($spSite.RootWeb.Url)
$curLine = “</ul>”
$curLine | Out-File $outFile -Append
$spSite.Dispose()
}
$curLine = “</body></html>”;
$curLine | Out-File $outFile -Append
Invoke-Item $outFile