Enumerate SharePoint sites and subsites using PowerShell

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!!!

}

}

}

Advertisements

7 Responses to “Enumerate SharePoint sites and subsites using PowerShell”


  1. 1 San May 10, 2009 at 2:12 am

    How do I run this script

  2. 3 James December 30, 2009 at 10:54 am

    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?

    • 4 andrei338 December 31, 2009 at 4:25 pm

      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

      }

  3. 5 Michael July 8, 2010 at 2:13 pm

    Thanks for the code posting for enumerating all subsites of an application it works like a charm.

  4. 6 valygreavu November 28, 2011 at 2:11 am

    Thank’s for sharing. Save me lot of hours.


  1. 1 #SharePoint 2010 – Enumerate and Deactivate Anonymous access using PowerShell « Valy Greavu's Live Blog Trackback on November 28, 2011 at 2:08 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




RSS Information Week Headlines

  • An error has occurred; the feed is probably down. Try again later.

RSS SharePoint Team Blog

  • An error has occurred; the feed is probably down. Try again later.

RSS InfoPath Team Blog

  • An error has occurred; the feed is probably down. Try again later.

RSS Joel Oleson Blog

  • An error has occurred; the feed is probably down. Try again later.

RSS Susan Hanley’s KM Blog

  • An error has occurred; the feed is probably down. Try again later.

Blog Stats

  • 355,172 hits

%d bloggers like this: