Jump to content
Sign in to follow this  

List users from Active Directory

Recommended Posts


Pardon my ignorance:

I am trying to get a list of all users in a Domain (ADS). I only require the LoginName to be returned, no other information.

I have tried to make sense of some scripts I found on the forum, but I am having some difficulties in isolating the parts I need.

Any help would be most appreciated.


Share this post

Link to post
Share on other sites

Untested but should be close:

Local $objCommand = ObjCreate("ADODB.Command")
Local $objConnection = ObjCreate("ADODB.Connection")
$objConnection.Provider = "ADsDSOObject"
$objConnection.Open ("Active Directory Provider")
$objCommand.ActiveConnection = $objConnection
Local $strBase = "<GC://dc=MyDom,dc=Inc>"
Local $strFilter = "(&(objectCategory=person)(objectClass=user)(sAMAccountName=*))"
Local $strAttributes = "cn,sAMAccountName,displayName,sn,distinguishedName"
Local $strQuery = $strBase & ";" & $strFilter & ";" & $strAttributes & ";subtree"
$objCommand.CommandText = $strQuery
$objCommand.Properties ("Page Size") = 100
$objCommand.Properties ("Timeout") = 30
$objCommand.Properties ("Cache Results") = False
$objCommand.Properties ("searchscope") = $ADS_SCOPE_SUBTREE
Local $objRecordSet = $objCommand.Execute
While Not $objRecordSet.EOF
;~      $strName = $objRecordSet.Fields ("sAMAccountName").Value
;~      $strCN = $objRecordSet.Fields ("cn").value
;~      $strdisplayName = $objRecordSet.Fields ("displayName").value
;~      $strSN = $objRecordSet.Fields ("SN").value
;~      $strdistinguishedName = $objRecordSet.Fields ("distinguishedName").value
    FileWriteLine("Users.txt",$objRecordSet.Fields ("sAMAccountName").Value)
$objConnection = ""
$objCommand = ""
$objRecordSet = ""
Edited by JdeB

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
Live for the present,
Dream of the future,
Learn from the past.

Share this post

Link to post
Share on other sites

you can also do a dsquery command with the -o samid option.

dsquery -o samid

Same basic ends, different means.


Share this post

Link to post
Share on other sites

jdeb method is the fastest way

i've used it so much in my script

it's an ADO search, the same as the search engine of the tool provided by AD

now, you can do a lot more with ado, or dsquery (but i prefer ADO)

list groups, users, OrganisationalUnit....users of a group

-- Arck System _ Soon -- Ideas make everything

"La critique est facile, l'art est difficile"

Projects :

[list] [*]Au3Service : Run your exe as service V3 / Updated 29/07/2013 Get it Here [/list]

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...