search AD by firstname

the below brings every usename that starts with john.. but i want to search only first name.. how cna i do that..
dsquery user -name *john* -limit 0

the userid maybe jjohnson but the name maybe john johnson, and i want all employees whose name is john or like john (even if firstname is johnson or mohjohng
LVL 5
25112Asked:
Who is Participating?
 
Krzysztof PytkoConnect With a Mentor Active Directory EngineerCommented:
I'm sorry I made a mistake in a syntax :/ to separate attributes to display from dsquery command, do not use comas (,) just spaces :/ My fault

Yes, you should use *John* to get everything containing john in First Name

dsquery * -filter "&(&(objectClass=User)(objectCategory=Person)(givenName=*john*))" -limit 0 -attr name givenName sn sAMAccountName

Krzysztof
0
 
Bill PrewCommented:
Try this.

dsquery user -givenName john -limit 0

~bp
0
 
Krzysztof PytkoActive Directory EngineerCommented:
Use this

dsquery user -fn "John*" -limit 0 | dsget user -fn -ln -samid

Regards,
Krzysztof
0
Easily manage email signatures in Office 365

Managing email signatures in Office 365 can be a challenging task if you don't have the right tool. CodeTwo Email Signatures for Office 365 will help you implement a unified email signature look, no matter what email client is used by users. Test it for free!

 
ReubenwelshConnect With a Mentor Commented:
Hi, have you used powershell before? Powershell gives you a lot more options for operations like this. You can then do "get-aduser -firstname John" and youll get them all :)
0
 
Krzysztof PytkoActive Directory EngineerCommented:
Oh sorry, it's available only in dsget structure ;) In this case you have to use more advanced query

dsquery * -filter "&(&(objectClass=User)(objectCategory=Person)(givenName=John*))" -limit 0 -attr name,givenName,sn

Krzysztof
0
 
Bill PrewConnect With a Mentor Commented:
Sorry, I suspect my syntax isn't quite right, I tend to use ADFIND mostly, find it easier.  But I think the bottom line is you want to use the givenName attribute to filter by firt name, not the Name attribute.

~bp
0
 
Krzysztof PytkoActive Directory EngineerCommented:
and if you're interested also with login in the output add als sAMAccountName at the end of query

dsquery * -filter "&(&(objectClass=User)(objectCategory=Person)(givenName=John*))" -limit 0 -attr name,givenName,sn,sAMAccountName

Krzysztof
0
 
ComputerBeastConnect With a Mentor Commented:
Hi all,
Try out the script:

On Error Resume Next

Const ADS_SCOPE_SUBTREE = 2

Set objConnection = CreateObject("ADODB.Connection")
Set objCommand =   CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection

objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE

objCommand.CommandText = _
    "SELECT sAMAccountName FROM 'LDAP://dc=fabrikam,dc=com' WHERE objectCategory='user' " & _
        "AND givenName='John'
Set objRecordSet = objCommand.Execute

objRecordSet.MoveFirst
Do Until objRecordSet.EOF
    Wscript.Echo objRecordSet.Fields("sAMAccountName").Value
    objRecordSet.MoveNext
Loop

Hope it helps.
Thank you
Anil
0
 
25112Author Commented:

thanks..

this is xp.. so no powershell possibility.

iSiek, should i made "givenName=*John*)" instead of "givenName=John*)" to account for "mohjohng" Firstname?

Is it the same (givenName='John' ) as above for ComputerBeast's code?
0
 
25112Author Commented:

dsquery * -filter "&(&(objectClass=User)(objectCategory=Person)(givenName=John*))" -limit 0 -attr name,givenName,sn,sAMAccountName
seems to bring something back, but is blank.. can see any content.. but i can see it tried to bring something back..
0
 
ReneGeCommented:
Are you trying this while logged in with sufficient credentials?
0
 
25112Author Commented:
I can see other dsquery results.. only the above one  is coming as blank
0
 
Mike KlineConnect With a Mentor Commented:
Make it *john* and that should work, I created a test user to verify(see screenshot)

Thanks

Mike
dsquery-john.jpg
0
 
25112Author Commented:
thanks Mike.

dsquery user -name *john* -limit 0
works for me, and brings back results but it queries on the username and not real name.

but
dsquery * -filter "&(&(objectClass=User)(objectCategory=Person)(givenName=*john*))" -limit 0 -attr name,givenName,sn,sAMAccountName
works but i cant see the results.
0
 
Mike KlineCommented:
would you be willing to try another free tool to test with

adfind   http://www.joeware.net/freetools/tools/adfind/index.htm

query would almost look the exact same just want to see if you get different results.

Thanks

Mike
0
 
25112Author Commented:
sure- OK, so I tried
AdFind -f "name=*john*"

but i get back
ldap_get_next_page_s: Error 0x55 (85) - Timeout

0 Objects returned
0
 
25112Author Commented:
but
dsquery user -name *john* -limit 0
is lightning fast..
but does not give firstname or lastname
0
 
Mike KlineCommented:
ok tanks for downloading

try

adfind -default -f "&(objectcategory=person)(objectclass=user)(givenname=*john*)" samaccountname givenname


Thanks


Mike
0
 
25112Author Commented:
thanks.. but same timeout issue as lastime..
0
 
25112Author Commented:
that was it! thanks a lot...
0
 
Krzysztof PytkoActive Directory EngineerCommented:
You're welcome :)

Krzysztof
0
All Courses

From novice to tech pro — start learning today.