Search LDAP Entries with ldapsearch on Linux/Unix

RedHat ldapsearch Documentation

ldapsearch -D "CN=<MyUser>,OU=S,OU=Useraccounts,DC=de,DC=<COMPANY>,DC=com" -w "PASSWORD" -b "DC=<MyCompany>,DC=com" 
   -s sub "(& (objectClass=user) (name=TestUser1))" -h "" -p "3268" telephonenumber name department

# Result:
# extended LDIF
# LDAPv3
# base <DC=<MyCompany>,DC=com> with scope subtree
# filter: (& (objectClass=user) (name=TestUser1))
# requesting: telephonenumber name department

# TestUser, M, Useraccounts, XX,
dn: CN=TestUser1,OU=M,OU=Useraccounts,OU=XX,DC=emea,DC=MyCompany,DC=com
department: <HIDDEN>
name: TestUser1
telephonenumber : 0711-XXXXXXXXX

# search result
search: 2
result: 0 Success

# numResponses: 2
# numEntries: 1

-D     Username (BaseDN) that is used to connect to ldap
-w     Password to connect to ldap
-b     Specifies the starting point for the search
-s     Specifies the scope of the search
sub  search also in subtree
(& (objectClass=user) (name=mcp8wz)) = filter for objects with objectClass=user and where attribute „name“ = mcp8wz
-h     hostname of LDAP Server
-p     Port of LDAP Server
at the end of the command you can add all attributes you would like to fetch from LDAP. Leave empty to retrieve all information


Search LDAP Entries using Powershell

Below little script shows how to connect to Active Directory / LDAP and search for entries.


$mySearcher = New-Object System.DirectoryServices.DirectorySearcher

# it is possible to specify manually a ldap search Path:
#$mySearcher.SearchRoot = "LDAP://DC=de,DC=myComapny,DC=com"

# or to get current ldap path (for example: DC=de,DC=myComapny,DC=com)
$objDomain = New-Object System.DirectoryServices.DirectoryEntry

$mySearcher.SearchRoot = $objDomain

# search for object class "user" with attribte "name=TestUser1"
$mySearcher.Filter = "(& (objectClass=user) (name=TestUser1))"
$mySearcher.SearchScope = "sub"
$mySearcher.PageSize = 10

# specifiy the attributes you would like to retrieve
$myAttributes = ("telephonenumber", "mail", "department")
# comment below line to get all attributes

$abc = $mySearcher.FindAll()

# show all attributes and values
foreach ($i in $abc.Properties.PropertyNames){
    Write-Host $i , "=" , $abc.Properties.$i

The Script will generate the following output:

mail =
telephonenumber = +49XXXXXXXX
department = MyDepartment
adspath = LDAP://<removed_by_author>