Script That Displays Group Membership and Active Directory Location

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 & _
    objMember.Parent
Next
Set objFile = Nothing
Set objFileSystem = Nothing
Set objGroup = Nothing

4 Comments

  1. john dito says:

    Save as .bat or .vbs??

    Thanks

  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 comment

*