February 10, 2012

Powershell script to List All Sharepoint Site collections and sub sites

Hi,

i came up with an requirement to gather all the site collections and sites and sub sub sites in our 2007 & 2010 environment. so decided to prepare a poweshell script which can list all site collection and it's sub webs list under the site collection and a Final count. the script as follows:

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") > $null
$siteUrl = Read-Host "Enter Site URL"
$rootSite = New-Object Microsoft.SharePoint.SPSite($siteUrl)
$spWebApp = $rootSite.WebApplication
$count = 0
           write-host "Site Collections"
           write-host ""
    foreach ($site in $spWebApp.Sites) {
        $count++
        write-host "Site Collections URL --> -->" $site.URL
           write-host ""
           write-host "SubSites"
           write-host ""
    foreach ($web in $site.AllWebs) {
        $count++
        write-host "SubSite URL --> --> -->" $web.URL 
        write-host ""
    } 
}

write-host "Total Count :" $count

Hope this script help in your requirement too.

Regards,
JK

8 comments:

  1. Thanks JK, This post was helpful. If you are using SharePoint 2010 and need just the total count of site collections, I came up with the following one liner:

    Get-SPWebApplication | ForEach-Object { "{0} has {1} site collection(s)" -F $.Name, $.Sites.Count }

    ReplyDelete
  2. Thank you talbottcrowell for your contribution, definetly will consider this one line command when required only count.

    ReplyDelete
  3. Please excuse my utter ignorance, my knowlewdge of Powershell is minimal (I'm learning). Am I right in thinking that that will only list the sites at the top level of the site collection?

    I have 'inherited' a server where sites have been created as subsites of existing sites with no real logic, past administrators have seemed to create sites at whatever level/position they were at the time with no thought for URL length or the relationship between sites. We are looking to move these sites to a new server and need to identify what sites there are and when they were last accessed (many of them were created for a purpose some time ago and either never used or used for a time then fell into disuse) so we can clear out the sites that aren't used and get some proper organoisation to the sites which are.

    Should we be able to get this information via Powershell?

    Thanks

    Stephen

    ReplyDelete
    Replies
    1. Hi Stephen,

      the script which i have provided will take you to the Root to the end branch recursively:

      let say you web application http://webapplication

      http://webapplication [is a root site collection]

      http://webapplication/subsite1
      http://webapplication/subsite1/subsite1.1
      http://webapplication/subsite1/subsite1.1/subsite1.2

      http://webapplication/subsite2
      http://webapplication/subsite2/subsite2.1

      and so on....

      and if you want move your sub sites to some other site collection / to another farm, then explore to "Export-SPWeb / stsadm -o export" commands.

      or you want to move entire site collection to new web application / new farm, then explore
      "stsadm -o backup" / "Backup-SPSite" reffer HERE

      and to get the details of last access date you need to explore SQL Commands to execute at the contentDb level.

      i hope this information helped you.

      Thanks,
      JK

      Delete
  4. Hi Girish, I have some very large sites in my MOSS 2007 x64 and need to report on those. Do you have a script that exports a list of all sites with all the doc libraries with size? I need to identify all the large sites and then move them into their own site collections as all the big sites are sitting in one site collection and our content database has grown to 400+ GB. Any suggestion would be greatly appreciated. If you have a powershell script that can assist me with exporting all the large sites contents and then import those into a different web application and site collection that will be great. I am also worried how to move large sites that are in excess of 32 GBs, in case the export and import have any limitations or if it times out while trying to export.
    Thanks,
    Anant

    ReplyDelete
    Replies
    1. Hi Anant,

      1) for your first requirement to identify Large sites, Contents from the web application:

      i suggest you to use a Freeware tool from Axceler PinPoint, even were into same situation, and we are generating reports using this tool past from one and half year. its a great tool to posses.

      2) And with your second requirement:
      why don't you move all your Site Collections into a different ContentDB's instead letting them reside in one contentDB.

      Please do explore stsadm -help mergecontentdbs command. and do follow simple 10 steps which i have followed and executed them in our Non-Production environment. Find the steps as per the Document

      Thanks,
      JK

      Delete
  5. Hi Jk,

    This is very useful article.
    I am working on automation of New web application creation, New Site Collection that we generally do from central admin. I am doing this using PowerShell in MOSS 2007, I have installed PS on my MOSS 2007 servers.
    This process is simple in SharePoint 2010, can you please tell me a script to create Webapplication, Site Collection by using powershell in MOSS 2007.

    Thanks,
    Ram

    ReplyDelete
    Replies
    1. Hi Ram,

      please do explore this CodePlex project.

      Thanks,
      JK

      Delete