Home Contact

The Frog Pond of Technology

Ripples of Knowledge for SharePoint and Other .Net Technologies

News

 Subscribe to this blog


About Me

Name:
Brian T. Jackett
Location:
Columbus, OH
Company:
Microsoft

Find me on...

Tag Cloud


Archives

Post Categories

Syndication:

PowerShell Script To Traverse All Sites In SharePoint 2010 (or 2007) Farm

    Over the past few years I’ve written a number of blog posts on performing various actions against a site collection or web application (display site collection admins, find all SPShell admins with database, find closed web parts).  Invariably with every post I get some comments along the lines of “this is great, how can I run this against every site in the farm”.  Well today you get your wish (sort of).  Below you will find a template script that traverses all sites within your local farm.  Isn’t that great!?!

    In it’s current state this script will simply output the title and URL of every site within the farm.  You may modify the function to perform your desired actions.  One stipulation is that you must have proper access to each of the web applications / site collections in order to actually traverse them.  Please leave any feedback that you have on this template in the comments.

 

Scripts

SharePoint 2010

Download the SharePoint 2010 template here.

 

Here is the source as well

function RecurseSiteAndDoSomething() {
    param([Microsoft.SharePoint.SPWeb]$SiteIdentity)

    Write-Output "Site: $($SiteIdentity.Url)"
    
    if($SiteIdentity.Webs.Count -gt 0)
    {
        foreach($subWeb in $SiteIdentity.Webs)
        {
            RecurseSiteAndDoSomething -SiteIdentity $subWeb
        }
    }
}

$contentWebAppServices = (Get-SPFarm).services |
 ? {$_.typename -eq "Microsoft SharePoint Foundation Web Application"}

foreach($webApp in $contentWebAppServices.WebApplications)
{
    Write-Output "Web Application: $($webApp.name)"
    foreach($siteColl in $webApp.Sites)
    {
        Write-Output "Site Collection: $($siteColl.Url)"
        RecurseSiteAndDoSomething -SiteIdentity $($siteColl.RootWeb)
    }
} 

SharePoint 2007

I am still ironing out a few things with the SharePoint 2007 version, but here is the current script.

 

function RecurseSiteAndDoSomething() {
    param([Microsoft.SharePoint.SPWeb]$SiteIdentity)

    Write-Output "Site: $($SiteIdentity.Url)"
    
    if($SiteIdentity.Webs.Count -gt 0)
    {
        foreach($subWeb in $SiteIdentity.Webs)
        {
            RecurseSiteAndDoSomething -SiteIdentity $subWeb
        }
    }
}

$contentWebAppServices = ([Microsoft.SharePoint.Administration.SPFarm]::Local).services | 
? {$_.typename -eq "Microsoft SharePoint Foundation Web Application"}

foreach($webApp in $contentWebAppServices.WebApplications)
{
    Write-Output "Web Application: $($webApp.name)"
    foreach($siteColl in $webApp.Sites)
    {
        Write-Output "Site Collection: $($siteColl.Url)"
        RecurseSiteAndDoSomething -SiteIdentity $($siteColl.RootWeb)
    }
} 

 

Conclusion

    In this post I presented a template script for recursively traversing each site within the local SharePoint farm.  This is just a first pass at this template.  If I make any updates in the future I will update this post to reflect.

 

      -Frog Out


Monday, September 19, 2011 6:28 AM

Feedback

# re: PowerShell Script To Traverse All Sites In SharePoint 2010 (or 2007) Farm

Thanks for the post, I used this post to create a script that runs another script on all sites in the Farm. You can see the post at: http://derbium.wordpress.com/2011/10/19/running-a-script-on-all-sites-in-a-web-application/ 10/19/2011 1:22 PM | derbium

# re: PowerShell Script To Traverse All Sites In SharePoint 2010 (or 2007) Farm

My requirement is that I want to list out all the site collections which are having same site collection administrator. How can I get solution of it using powershell 6/12/2012 1:26 PM | Ankit

# re: PowerShell Script To Traverse All Sites In SharePoint 2010 (or 2007) Farm

Ankit,

You can use the output from my script for finding site collection admins in a web app. Run a compare to find if any are listed multiple times.

http://briantjackett.com/archive/2011/03/25/powershell-script-to-display-all-sharepoint-site-collection-administrators-in.aspx
6/29/2012 10:21 AM | Brian T. Jackett

# re: PowerShell Script To Traverse All Sites In SharePoint 2010 (or 2007) Farm

Hi,

I want to check if my all site collection and subsites under them are up and running we have around 2000 sites.

Any script for this. 11/21/2012 12:43 AM | mohd

# re: PowerShell Script To Traverse All Sites In SharePoint 2010 (or 2007) Farm

MOHD,

What would classify as "up and running"? Do you mean responds to a PING request, sends back a success HTTP 200 response, etc.?

I do not have any scripts for those types of scenarios. You would likely be better off using monitoring software such as System Center Operations Manager, Solar Winds, or the like to monitor your site rather than a PowerShell script. They are much more robust and offer alerting and automated remediation functionality.
11/21/2012 10:21 AM | Brian T. Jackett

Post A Comment
Title:
Name:
Email:
Comment:
Verification: