List all SharePoint site collections and sub sites recursively

Posted: November 24, 2009 in Site Collection List

As a SharePoint developer or an administrator first thing that comes into our mind when we think of getting the list of site collection and subsites will be using stsadm command line utility.  You can use the command

stsadm.exe -o enumsites -url <root url>

to get the list  of site collections and

stsadm.exe -o enumsubwebs -url <root url>

to get the list of sub sites.

It will be great if we combine both of these. Here is the VB script code to  do this which will create an XML file that you can open in an excel file.

Option Explicit
Const STSADM_PATH =”C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN\stsadm”
Const ROOT_URL = “
Const FILE_NAME = “G:\listofsites.xml”
Dim objShell, objExec, objXml, objXml2,objXml3, objSc,objFso, objFile, objWeb
Dim strResult, strSubResult, strUrl, strCmd, strOwner, strXML, returnValue, returnValue1 
‘Retrieves all site collections in XML format.
WScript.Echo “Creating shell object and calling root enumsites command”
Set objShell = CreateObject(“WScript.Shell”)
Set objExec = objShell.Exec(STSADM_PATH & ” -o enumsites -url ” & ROOT_URL)
strResult = objExec.StdOut.ReadAll
‘Load XML in DOM document so it can be processed.
WScript.Echo “Loading XML File”
Set objXml = CreateObject(“MSXML2.DOMDocument”)
Set objXml2 = CreateObject(“MSXML2.DOMDocument”)
Set objXml3 = CreateObject(“MSXML2.DOMDocument”)

WScript.Echo “Creating File System Object”
‘Create the FileSystemObject and write to file.
Set objFso = CreateObject(“Scripting.FileSystemObject”)
if objFso.FileExists(FILE_NAME) then
    objFso.DeleteFile FILE_NAME, True
end if
set objFile = objFso.CreateTextFile(FILE_NAME, True)
‘Loop through each site collection and call enumsubwebs to get the child URL’s.
For Each objSc in objXml.DocumentElement.ChildNodes
    strUrl = objSc.Attributes.GetNamedItem(“Url”).Text
    strOwner = objSc.Attributes.GetNamedItem(“Owner”).Text
    strCmd = STSADM_PATH & ” -o enumsubwebs -url “”” + strUrl + “”””
    Set objExec = objShell.Exec(strCmd)
    strResult = objExec.StdOut.ReadAll
    objFile.WriteLine(“<SITECOLLECTION SiteCollectionURL='” & strUrl & “‘ Owner = ‘” & strOwner & “‘>”)
    WScript.Echo “Traversing the sub Webs…”
    call GetSubSites(strResult)
set objFile = nothing
set objFso = nothing
set objXml = nothing
set objXml2 = nothing
set objXml3 = nothing
set objExec = nothing
WScript.Echo “File created”
sub GetSubSites(strResult)
returnValue1 = objXml2.LoadXML(strResult)
if returnValue1 = true then
for Each objWeb in objXml2.DocumentElement.ChildNodes
    strCmd = STSADM_PATH & ” -o enumsubwebs -url “”” + objWeb.text + “”””
    Set objExec = objShell.Exec(strCmd)
    strResult = objExec.StdOut.ReadAll
    returnValue = objXml3.LoadXML(strResult)
    if returnValue = true then
 if objXml3.DocumentElement.Attributes.GetNamedItem(“Count”).Text <> “0” Then
 WScript.Echo strResult
 call GetSubSites(strResult)
 end if
    end if

end if
end sub


ROOT_URL  will be your farm’s root URL.

FILE_NAME will be the path for output XML .

STSADM_PATH will be the path where yout stsadm.exe resides.

After creating the script file run it in command prompt using the command

cscript.exe ListAllMossSites.vbs

This will create a XML file. Open this in an excel. You will have all the details.

  1. […] This post was mentioned on Twitter by SharePoint, Chanakya Jayabalan. Chanakya Jayabalan said: List all SharePoint site collections and sub sites recursively: […]

  2. Peter says:

    Is anyone getting a document error on this?

  3. Terri says:

    I’m getting the same error as Peter above. What could be the problem?

    • @Peter, @Terry,
      When you copy the code, make sure all the quotes (single or double) are replaced properly..

      • Charlie says:

        Im still getting that error – Microsoft VBScript runtime error: Object required: ‘objXml3.DocumentElement’

        It works on my test environment. Is it due to special characters or the size of the array? Its fails at 77 subwebs

  4. Tom says:

    Did anyone solve this? Getting same runtime error “Object required: ‘DocumentElement'” on line 42 char 1

    • Tom says:

      OK I had a look at it and it seems that I have been using a wrong ROOT_URL. If the script is failing for you as well, try adding this around line 18 (after it says: strResult = objExec.StdOut.ReadAll)

      msgbox strResult

      which should give you an XML output, if not, then try if
      stsadm -o enumsites -url http://siteurl
      is giving you an error message

      In my case I had to use the Sharepoint IP address instead of the FQDN (example:

      Thanks for the script

  5. I’ve been exploring for a little bit for any
    high-quality articles or blog posts in this kind of house .

    Exploring in Yahoo I ultimately stumbled upon this web site.
    Reading this information So i’m glad to express that I’ve a very just right uncanny feeling I
    discovered exactly what I needed. I so much unquestionably will
    make sure to don?t disregard this web site and give it a look
    on a relentless basis.

  6. Hurra, que es lo que estaba buscando explorando para, qué cosas Material!
    Presente aquí en este página web, gracias administrador de esta página web sitio web.

  7. Melissa C says:

    Worked perfectly after I replaced the quotes. Thank you so much for sharing this.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s