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
Originally posted on: https://briantjackett.com/archive/2011/09/19/powershell-script-to-traverse-all-sites-in-sharepoint-2010-or.aspx#597334Thanks 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/
LikeLike
Originally posted on: https://briantjackett.com/archive/2011/09/19/powershell-script-to-traverse-all-sites-in-sharepoint-2010-or.aspx#614980My 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
LikeLike
Originally posted on: https://briantjackett.com/archive/2011/09/19/powershell-script-to-traverse-all-sites-in-sharepoint-2010-or.aspx#615765Ankit,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.https://briantjackett.com/archive/2011/03/25/powershell-script-to-display-all-sharepoint-site-collection-administrators-in.aspx
LikeLike
Originally posted on: https://briantjackett.com/archive/2011/09/19/powershell-script-to-traverse-all-sites-in-sharepoint-2010-or.aspx#621763Hi,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.
LikeLike
Originally posted on: https://briantjackett.com/archive/2011/09/19/powershell-script-to-traverse-all-sites-in-sharepoint-2010-or.aspx#621786MOHD, 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.
LikeLike
Thanks for the post Brian. I like the recursive nature of this script to return subsites as well. Well done.
LikeLike
[…] posts for “PowerShell to Enumerate SharePoint 2010 or SharePoint 2013 Permissions” or “PowerShell Script To Traverse All Sites In SharePoint 2010 (or 2007) Farm” to assist with traversing through all sites within a SharePoint on-prem farm. In this post […]
LikeLike