Exporting last name, first name and username from Active Directory using AdFind

AdFind is a Windows command line Active Directory query tool. It is a mixture of ldapsearch, search.vbs, ldp, dsquery, and dsget tools with a ton of other cool features thrown in for good measure.

This post describes how I managed to export

  • Last name (surname/sn)
  • First name (givenName)
  • Username (samaccountname)

from a Active Directory and save the result to a comma separated file (CSV)

adfind.exe -b ou=ActiveDirectory,dc=example,dc=com -f "objectClass=user" sn givenName  samaccountname -nodn -adcsv > exported_users.csv

The result of this command is as follows

"Last name","First name","username"

This is a nicely formatted csv file that makes it easy to work with.

What values are available and can be searched for in a Active Directory?
If you are uncertain on the name of what you are looking for, then this line comes handy

adfind.exe -b ou=ActiveDirectory,dc=example,dc=com -s subtree |more


Remember to use the | more at the end of the line because this command lists a lot of Active Directory content.

The result of this command can contain some of the following values

cn, sb,giveNnAME,  distinguishedname, instanceType, whenCreated, whenChanged, displayName, uSNCreated, memberOf, uSNChanged, department, homeMTA, proxyAddresses, homeMBD, mDBUseDefaults, mailNickName, name, objectGUID, userAccountControl, badPwdCount, codePage, countryCode, homeDirectory, homeDrive, badPasswordTime, lastLogoff, lastLogon, logonHours, pwdLastSet, primaryGroupID, userParameters, profileParh, objectSid, accountExpires, logonCount, sAMAccountname, SamaccountType, showInAddressBook, msNPAllowDialoin, dSCorePropagationData, lastLogonTimestamp, textEncodeORAddress, mail, middleName, msExchaPoliciesExcluded, msExchHomeServerName, msExchALObjectVersion, msExchMailboxSecurityDescriptor, msExchUserAccountControl, msExchMailboxGuid

The values above are usually assosiated with a useraccount.

5 thoughts on “Exporting last name, first name and username from Active Directory using AdFind

  1. Joe

    I am trying to use Adfind to solve an issue for HR. Each month we receive a CSV file from HR that lists the emloyees who no longer work for our company. The CSV file contains simply their first and last name in two columns. What I would like to do is is run Adfind against this CSV file and output their First Last and samAccount name to a new CSV file.

    How would I do this?

      1. Joe

        Ryan,

        I have actually figured this out using a powershell script. If can share this with you if you want. Please let me know.

Comments are closed.