Listing groups and users from folders

Discussion in 'Scripting' started by Randolph Clay, Aug 26, 2003.

  1. I need help in creating a script that can list all the
    user and group memberships of a given folder. This wll
    also apply to subfolders beneath the root folder. Info
    needs to be extracted to a file.

    Randolph Clay, Aug 26, 2003
    1. Advertisements

  2. Hi,

    I will assume by folder you mean a container or OU in Active Directory.
    Reply if this is not the case. The VBScript program below outputs the NT
    name (sAMAccountName) of all users and groups in a container or any child
    containers. The Distinguished Name of the container is hard coded. The
    output is indented to show the hierachial structure of the containers and
    their contents.

    Option Explicit
    Dim strContainerDN, objContainer

    strContainerDN = "ou=Sales,dc=MyDomain,dc=com"
    Set objContainer = GetObject("LDAP://" & strContainerDN)

    Call EnumContainer(objContainer, "")

    Sub EnumContainer(objParent, strOffset)
    ' Recursive subroutine to enumerate containers.
    Dim objGroup, objUser, objChild

    ' Output container name
    Wscript.Echo strOffset & "Container/OU: " &

    ' Enumerate groups in container.
    objParent.Filter = Array("group")
    For Each objGroup In objParent
    Wscript.Echo "--" & strOffset & " Group: " & objGroup.sAMAccountName

    ' Enumerate users in container.
    objParent.Filter = Array("user")
    For Each objUser In objParent
    Wscript.Echo "--" & strOffset & " User: " & objUser.sAMAccountName

    ' Enumerate child containers.
    objParent.Filter = Array("container","organizationalUnit","builtinDomain")
    For Each objChild In objParent
    Call EnumContainer(objChild, "--" & strOffset)

    End Sub

    Rather than using FSO to write the info to a file, it is easier to redirect
    the output to a text file. At a command prompt, run the VBScript program
    with the cscript host. For example, if the VBScript is called DocCont.vbs:

    cscript //nologo DocCont.vbs > Container.txt
    Richard Mueller [MVP], Aug 27, 2003
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.