How to list all the sub-sites and the site collections within a SharePoint web application using Windows Powershell

Recently, I worked on a partner's request for enumerating all the sub-sites and the site collection within a SharePoint web application. If you want to do the same thing, may this be useful for you.

Windows Powershell has made it so easy to work/administer with several Microsoft products like, SharePoint, SQL Server, Exchange Server etc., so that almost anyone who has some knowledge of Powershell programming can work with them.

Find out yourself by seeing the code below:

--------------------------------------------------

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") > $null

$farm = [Microsoft.SharePoint.Administration.SPFarm]::Local
$websvcs = $farm.Services | where -FilterScript {$_.GetType() -eq [Microsoft.SharePoint.Administration.SPWebService]}
$webapps = @()

foreach ($websvc in $websvcs) {
           write-host "Web Applications"
           write-host ""
    foreach ($webapp in $websvc.WebApplications) {
        write-host "Webapp Name -->"$webapp.Name
           write-host ""
           write-host "Site Collections"
           write-host ""
    foreach ($site in $webapp.Sites) {
        write-host "Site URL --> -->" $site.URL
           write-host ""
           write-host "Websites"
           write-host ""
    foreach ($web in $site.AllWebs) {
        write-host "Web URL --> --> -->" $web.URL
           write-host ""
           write-host "Lists"
           write-host ""
    foreach ($list in $web.Lists) {
           write-host "List Title --> --> --> -->" $list.Title
           write-host ""
    }

    foreach ($group in $web.Groups) {
           write-host "Group Name --> --> --> -->" $group.Name
           write-host ""

    foreach ($user in $group.Users) {
           write-host "User Name --> --> --> -->" $user.Name
           write-host ""
    }
    }

    }

    }

    }
}

--------------------------------------------------

Happy Programming!

Comments

  • Anonymous
    December 22, 2009
    Hi Vijay, I have a WSS 3.0 server with several webapps.  How can I modify this script to enumerate the sibsites under one of the webapps only? Thanks

  • Anonymous
    November 04, 2010
    Thanks Vijay, this is is exacly what i was looking for this morning.  Much appreciated

  • Anonymous
    April 29, 2011
    Anyway to run this script from a machine that is not a SharePoint server?

  • Anonymous
    June 14, 2011
    How can I export output to txt or csv file?

  • Anonymous
    November 17, 2011
    Its not working for me, :( can you help a little more, as I'm new to SP 2010

  • Anonymous
    July 06, 2012
    Do you have few commands to check it ?

  • Anonymous
    March 04, 2013
    fulfill your dreams (11551) we gives you the easiest way to increase income For more details visit: http://eazy2earn.com

  • Anonymous
    December 19, 2013
    Really Nice Post. It's Really Helpful. Thank  you for sharing... For more details visit: http://easy2cash.wordpress. com

  • Anonymous
    November 09, 2014
    Thank you for this post Can you help me? I need collect data from sharepoint 2007 and 2010/ Data must be: List off all application, site collection, subsite must consist of: Site name, Site URL, people with full control permission in site, last change data I need this information to understand which sites need to migrate and which inactive and can be remove. Thank you in advance

  • Anonymous
    December 03, 2014
    Thank You very much , Very good one!  this is what exactly I am looking for

  • Anonymous
    June 15, 2015
    Can anyone suggest additional code to add to get this to output to a CSV file?