Script That Displays Group Membership and Active Directory Location

Filed Under (Active Directory, Scripting) by on 15-12-2008

The following code can be run to display the group membership of an Active Directory group and also let you know each member’s LDAP Distinguished Name.  The output will name the text file the group name and will include all the members and their location in Active Directory.  Just copy this into a txt file and rename to .vbs  Enjoy!

Set objGroup = GetObject(“LDAP://cn=GroupName,ou=OUName,DC=DomainName,DC=local“)
Set objFileSystem = CreateObject(“Scripting.FileSystemObject”)
Set objFile = objFileSystem.OpenTextFile(objGroup.Get(“name”) & ” – Members.txt“, 2, True, 0)
For Each objMember in objGroup.Members
  objFile.WriteLine objMember.Get(“sAMAccountName”) & VbTab & _
    objMember.Get(“cn”) & VbTab & _
Set objFile = Nothing
Set objFileSystem = Nothing
Set objGroup = Nothing

4 Responses to “Script That Displays Group Membership and Active Directory Location”

  1.   john dito Says:

    Save as .bat or .vbs??



  2.   BrianM Says:

    Sorry that would be a .vbs


  3.   JohnD Says:

    Hmmm craps out on line 2 Char expected statement
    code 800A0400
    compilation error


  4.   BrianM Says:

    Make sure you change (“LDAP://cn=GroupName,ou=OUName,DC=DomainName,DC=local”) to use the DN of a group that you want to use this on in your network. Are you sure the DN is correct for your group?


Leave a Reply