Thursday, August 9, 2012

Get all SharePoint sites (webs) recursively in PowerShell

SharePoint has always had confusion between sites and webs. What we are really talking about hear is for a given site (not site collection, but really a web) such as a team site we want to get all the sites below it. An easy way to do this is to use PowerShell if you are using SharePoint 2010.

  1. Open SharePoint 2010 Management Shell (As Administrator).
  2. Paste the snippet of code below into the prompt. Note, the first line can be uncommented if you are just using a PowerShell prompt that doesn’t have the SharePoint PowerShell commands added already.

 

#Add-PSSnapin "Microsoft.SharePoint.PowerShell"
$url = "
http://mysharepoint/SiteDirectory/site1"
$site
= Get-SPWeb ($url)
foreach ($web in $site.Site.AllWebs)
{
    if ($web.Url.StartsWith($url))
    {
    Write-Host ($web.Name + "|" + $web.Url)
    }
}

In my example I am writing to the console the name of the site, the pipe character as a delimiter, and then the url.

2 comments:

Anonymous said...

This is not recursively.. but works

Anonymous said...

For me in 2013 in does not all work) but works:

$url = "http://mysharepoint/SiteDirectory/site1"
$site = Get-SPWeb ($url)
$site.Site.AllWebs

Thanks!

Best regards,
Gennady