Ldap query to find all users with a certain alias

What query can be used to find all users that have an alias with @domainB.com?  I see how to find the primary ones, but not aliases.  If the query could also return the users name and what all the aliases are, that would be useful as well.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris DentPowerShell DeveloperCommented:
Hey :)

This should do it:


Or did you want those that only have the alias?

You might be better having a script do this for you. Do you have any preference on scripting language?

TacoFlavoredKissesAuthor Commented:
vbscript is fine.

Any that have that, regardless of what others they have is fine.  Formatting it so we can tell what user has what would be great.
You could do this in a one liner if you use the powershell quest cmdlets for AD:


The Command below creates a csv file with a list of all users that have @domainb.com in their proxyaddress attribute as well as there accountname and displayname
get-qaduser -sizelimit 0 -ldapfilter "(proxyAddresses=*@domainB.com)" -IncludedProperties proxyAddresses | select samaccountname,displayname,proxyAddresses | export-csv c:\temp\Aliasinfo.csv

Open in new window

Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

slight change export-csv does not hand the proxy address so use out-file instead - not the best output but it should do the job

you should be able to import the file into excel format by opening excel and then importing the txt file using fixed sized fields as the delimiter./

Good luck

get-qaduser -sizelimit 0 -ldapfilter "(proxyAddresses=*@domainB.com)" -IncludedProperties proxyAddresses | select samaccountname,displayname,proxyAddresses | out-file c:\temp\aliasinfo.txt -width 1000

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TacoFlavoredKissesAuthor Commented:
Not currently using powershell in this exchange 2003 environment.  
Even though it is exchange 2003 you can still use the quest powershell cmdlets on an xp machine to complete the query. No need to install on the server as the attributes are stored in AD so you just need a PC and a standard user who will have read access to AD.

My VB skills are not great so if you need it in VB I will leave it to Chris :-)


Chris DentPowerShell DeveloperCommented:

Sorry, had to sleep.

I would also recommend PowerShell, it's lovely for tasks like this, far more accessible than VbScript.

I'll post a VbScript version in a couple of minutes so you have a full set :)


> slight change export-csv does not hand the proxy address

Force it into a string using a custom property and it'll be fine:

Get-QADUser -SizeLimit 0 -LdapFilter "(proxyAddresses=*@domainB.com)" `
  -IncludedProperties proxyAddresses | Select-Object samaccountname, `
    displayname, @{n='EmailAddresses';e={ "$($_.proxyAddresses)" }} | `
  Export-CSV c:\temp\Aliasinfo.csv

Has the same effect as [String]::Join(" ", $_.ProxyAddresses).

cheers Chris !
Chris DentPowerShell DeveloperCommented:
A quick VbScript version (delay in posting due to being a little busy this morning).

Const FILE_NAME = "Users.csv"
Const LDAP_FILTER = "(proxyAddresses=*@domainB.com)"

Dim objConnection : Set objConnection = CreateObject("ADODB.Connection")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"

Dim objCommand : Set objCommand = Createobject("ADODB.Command")
objCommand.ActiveConnection = objConnection
objCommand.Properties("Page Size") = 1000

Dim objRootDSE : Set objRootDSE = GetObject("LDAP://RootDSE")
objCommand.CommandText = "<LDAP://" & objRootDSE.Get("defaultNamingContext") & ">;" & _
  LDAP_FILTER & ";name,sAMAccountName,mail,proxyAddresses;subtree"

Dim objRecordSet : Set objRecordSet = objCommand.Execute

Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objFile : Set objFile = objFSO.OpenTextFile(FILE_NAME, 2, True, 0)


Do Until objRecordSet.EOF
  objFile.WriteLine objRecordSet.Fields("Name").Value & "," & _
    objRecordSet.Fields("SamAccountName").Value & "," & _
    objRecordSet.Fields("mail").Value & "," & _
    Join(objRecordSet.Fields("proxyAddresses").Value, " ")


Open in new window

TacoFlavoredKissesAuthor Commented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.