The Life of Brian

Active Directory, Group Policies, Server Core and the Life of Brian

Email Notifications

Blog Search Form

Go

Recent Posts

Tags

Disclaimer

  • This blog is provided "AS IS" with no warranties, and confers no rights. This blog contains my own views and does not necessarily reflect the view of my employer.
    Locations of visitors to this page
    Add to Technorati Favorites

Sites I Visit

Archives

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

Comments

john dito said:

Save as .bat or .vbs??

Thanks

# December 16, 2008 12:20 PM

BrianM said:

Sorry that would be a .vbs

# December 16, 2008 2:04 PM

JohnD said:

Hmmm craps out on line 2 Char expected statement

code 800A0400

compilation error

# December 17, 2008 2:11 PM

BrianM said:

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?

# December 17, 2008 2:16 PM