Skip navigation

Q. How can I create a summary of the contents of the organizational units (OUs) in my environment?

A. I recently needed to quickly document a client's OU structure for a domain and had to include in the documentation the number of users, groups, computers, and contacts in each OU. To achieve this, I wrote a short script--oulist.vbs--that uses Microsoft Active Directory Service Interfaces (ADSI) to interrogate Active Directory (AD) and produce a report that details the specified container's content. To use oulist.vbs, you can either save the following code into a file (name it oulist.vbs) or download the script.

Option Explicit

Dim strLdapPath, objConnection, objChild, dtmCreate
Dim totalUsers, totalComputers, totalGroups, totalContacts


' Check that all required arguments have been passed.
If Wscript.Arguments.Count 

Oulist.vbs calls the GetDetail function to check the content of the passed container. The script then checks for OUs in the current container and, for each OU it finds, calls the function again. A process that calls itself is known as a recursive process. Running oulist.vbs produces output on screen that's similar to the following:

C:\scripts>cscript oulist.vbs dc=savilltech,dc=net
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

 (0 users 0 groups 0 computers 0 contacts)
 - OU=Domain Controllers
   (0 users 0 groups 1 computers 0 contacts)
 - OU=test
   (0 users 0 groups 0 computers 0 contacts)
   - OU=subtest1
     (0 users 0 groups 0 computers 0 contacts)
     - OU=subsubtest1
       (0 users 0 groups 0 computers 0 contacts)
     - OU=subsubtest2
       (0 users 0 groups 0 computers 0 contacts)
   - OU=subtest2
     (0 users 0 groups 0 computers 0 contacts)
     - OU=subsub2test1
       (0 users 0 groups 2 computers 0 contacts)
     - OU=subsub2test2
       (1 users 1 groups 1 computers 1 contacts)
       - OU=subsubsubtest
         (0 users 0 groups 0 computers 0 contacts)

Totals: 1 users 1 groups 4 computers 1 contacts
Operation Completed

It's important to use the Cscript command; if you don't specify Cscript, every line of text in the output will pop up in a dialog box.

Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.