Active Directory query for username, first name, last name and email

Active Directory exposes query interface via OLE DB and ADO. The provider is "ADsDSOObject", the query syntax goes like this:

<LDAP://mydomain.com>;(objectType=user);givenname,sn

Perversely, the URL schema name LDAP must be capitalized.

Excel does not have a built-in ADO client, unless you code in VBA.

UPDATE: wrote a simple JavaScript query script for you:

var conn = new ActiveXObject("ADODB.Connection");
conn.Open("Provider=ADsDSOObject");
var rs = conn.Execute("<LDAP://your-domain.com>;(objectClass=user);sn,givenname");
var i;
if(!rs.EOF)
{
    rs.MoveFirst();
    while(!rs.EOF)
    {

        WScript.Echo(rs.Fields.Item("givenname")+","+rs.Fields.Item("sn")+"\n");
        rs.MoveNext();
    }
}

It queries the fiest and last name of all users in your domain. Place your domain name in the third line. Then save it as a .js file, and execute thusly:

cscript adquery.js >a.txt

And you'll end up with a text file called a.txt, with the names of your users, comma-separated. Import it into Excel or something.

In Excel, if you are willing to mess with macros, you can write a VBA function against ADO that performs the same query. Or use .NET's DirectorySearcher, recent versions of Excel let you consume .NET objects.


If you're using the .NET platform, I would suggest looking into the System.DirectoryServices namespace, which "provides easy access to Active Directory Domain Services from managed code."

MSDN also provides code samples for performing common tasks using System.DirectoryServices, available in both VB and C#. If you're familiar with one of these languages, you should hopefully be able to glean what you need (at least to get started, and then perhaps be able to ask other, more specific questions here on SO) from these examples.

Hope this helps!