Here’s a simple Powershell script that will enumerate all sites and their sub-sites in your SharePoint environment.
[System.Reflection.Assembly]::Load("Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c") | out-null
$oContentService = [Microsoft.SharePoint.Administration.SPWebService]::ContentService;
[Microsoft.SharePoint.Administration.SPWebApplicationCollection]$waColl = $oContentService.WebApplications;
$waColl1 = $waColl | where-object {$_.IsAdministrationWebApplication -eq $FALSE}
foreach ($wa in $waColl1)
{
$waName = $wa.Name
$sites = $wa.Sites
foreach ($obj in $sites)
{
$siteurl = $obj.Url.Replace("http://","").Replace(":","").split("/")
$site=new-object Microsoft.SharePoint.SPSite($obj.Url)
for($i=0; $i -lt $site.AllWebs.Count;$i++){
Write-Output $site.AllWebs[$i] | Select Url; ##Send through Pipeline
$site.Dispose(); ##ENFORCED DISPOSAL!!!
}
}
}
How do I run this script
First, install Powershell (http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx) on your computer. Then create a text file, copy the script into the text file and save it with .ps1 extension. Open command prompt, type “powershell ” followed by the full path to your script file.
Just curious – If I want to combine output into a CSV or XML file and Select members that are not of type System.String, How can I do this?
I added a variable $output = @() and concatenate the output of the select statement. But at the end when I channel the pipeline to XML or CSV, the original objects are already disposed of so only the data type is written to the CSV.
Is there a way to pipeline the select statement as only System.String data type?
Or, is it safe not for dispose of all of the objects and let regular garbage collection take care of them or is this a case of lost reference pointer and the obect never being cleaned up?
You could rewrite the script using foreach loops and write-host commands as follows. Then you can do a type check on each object before it’s sent to the output.
$site=new-object Microsoft.SharePoint.SPSite($obj.Url)
foreach ($web in $site.Allwebs )
{
# specify web properties to output, separated by “;”
write-host $web.URL , “;”, $web.Title, “;”, $web.ID
}
Thanks for the code posting for enumerating all subsites of an application it works like a charm.
Thank’s for sharing. Save me lot of hours.