Link to home
Start Free TrialLog in
Avatar of JayMulkey
JayMulkey

asked on

Using ACTIVE DIRECTORY how can I print out a list of all Users and their UserNames?

I have close to 400 users in AD and I don't want to have to look at them one at a time!   If I could get a list of just the UserNames I'd be in extacy!
ASKER CERTIFIED SOLUTION
Avatar of eric_bender
eric_bender
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of JayMulkey
JayMulkey

ASKER

Eric

I'm not as AD-savvy as you (did not understand much of what you said).
I will need to iterate through different OU's.

Thanks for your help.  This could save me a TON of time!

Jay

I use this script to give me a list of all the users & groups that exist beneath the OU in question.
As I commented this particular one writes the Group and Members (users) to a text file and each group is on one line, comma separated.
With a little modification. (do you script)  You can  tweak this one.  If not I can genericize it later.


Dim objBaseOU
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile("C:\touche.txt")

Set objBaseOU = GetObject("LDAP://OU=Touche Distribution Groups,DC=CU1,DC=ORG")

ProcessOU objBaseOU

Sub ProcessOU (ByRef objOU)
  Dim objUser, objGroup, objChildOU

  objOU.Filter = Array("user")
  WScript.Echo "Users in OU '" & objOU.Get("distinguishedName") & "':"
  For Each objUser In objOU
    WScript.Echo "  " & objUser.Get("distinguishedName")
  Next

For Each strName in arrNames
    objFile.write strpad + ","


  objOU.Filter = Array("group")
  WScript.Echo "Groups in OU '" & objOU.Get("distinguishedName") & "':"

  For Each objGroup In objOU
    WScript.Echo "  Members of group '" & objGroup.Get("distinguishedName") & "':"
    ProcessGroup objGroup, "    "
  Next

  objOU.Filter = Array("organizationalUnit")
  For Each objChildOU In objOU

    ProcessOU objChildOU
  Next
End Sub

Sub ProcessGroup (ByRef objGroup, ByVal strPad)
  Dim objMember

  For Each objMember In objGroup.Members
    If (LCase(objMember.Class) = "group") Then
      WScript.Echo strPad & objMember.Get("distinguishedName") & " (nested group)"
      ProcessGroup objMember, strPad & "  "
    Else
      WScript.Echo strPad & objMember.Get("distinguishedName")
    End If
  Next
End Sub
This does give you duplication though.  Let me work on just Iterating and sorting by OU.....
SOLUTION
Avatar of Rob Williams
Rob Williams
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ok,

So copy the script below into notepad.
You should then just need to change the entry for Set objFile to represent your text file, replace c:\touche.txt   with your location... the file does not need to exist it will be created.  
Secondly change the OU Entry (mine being Touche Distribution Groups) to the OU entry you need to use.  Then change the DC to be your domain... i.e. cu1.org relates as below.

If you have any questions let me know.


Dim objBaseOU
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile("C:\touche.txt")

Set objBaseOU = GetObject("LDAP://OU=Touche Distribution Groups,DC=CU1,DC=ORG")

ProcessOU objBaseOU

Sub ProcessOU (ByRef objOU)
  Dim objUser, objGroup, objChildOU

  objOU.Filter = Array("user")
 
  For Each objUser In objOU
      objFile.writeblanklines 1
    objFile.write " A " & objUser.Get("name")
   
  Next


  objOU.Filter = Array("group")
 

  For Each objGroup In objOU
 objFile.writeblanklines 1
    objFile.write objGroup.Get("name") + ","
   
    ProcessGroup objGroup, "    "
  Next

  objOU.Filter = Array("organizationalUnit")
  For Each objChildOU In objOU

    ProcessOU objChildOU
  Next
End Sub

Sub ProcessGroup (ByRef objGroup, ByVal strPad)
  Dim objMember

  For Each objMember In objGroup.Members
    If (LCase(objMember.Class) = "group") Then
      objFile.write strPad & objMember.Get("Name") + ","
      
    Else
      objFile.write strPad & objMember.Get("Name") + ","
      
    End If
  Next
End Sub

Once you have made the changes save the document and rename the suffix as a .vbs  instead of the default of .txt

Then you can just double click on the it and after a short time your file will be created.

All this does is using LDAP (Lightweight Directory Addressing Protocol--AD) to retrieve the arguments accordingly.

Make sure the drive/folder exists and that you have changed the OU and Domain.  You can add additional OU= entries if you have and wish to test on a downlevel nested OU.

If you want to make the change and post it I can look at it before you run it.
I had tried that utility in the past Rob, and for what I was trying to do it wasn't useful, but it may give Jay what he needs.
I seem to recall that it wouldn't define the users through the nested groups.  It's been a while... I use the originally posted script to export to Excel and the post as an HTM on our Intranet for Distribution Group purposes.  
net user /domain > userlist.txt

or, for full names,

dsquery user "dc=yourdomain,dc=com" -name * -limit 0 | dsget user -display > userlist.txt
Shift-3 much better and easier solution.  net user /domain is the absolute easiest, but doesnt give all the info you wanted.  Also if you are using Win2003 create a saved query and export the list to excel.
Thanx Let me know if you need any more help with the scripting.
Thanks JayMulkey. Good luck with it,
--Rob