Query Department field from AD users


  • The LDAP syntax query for all users that have a department in AD would be:



    For example, this can be used with dsquery at the command prompt of a DC:

    dsquery * -Limit 0 -filter "(&(objectCategory=person)(objectClass=user)(department=*))" -Attr sAMAccountName department > UserDepts.txt


    The above may word wrap, but it is one line. The -Attr parameter specifies the attributes to output. sAMAccountName is the "pre-Windows 2000 logon" name. You could output distinguishedName instead. The same filter could be used with Joe Richards' adfind, or a VBScript or PowerShell script.

    If it helps, I have a VBScript program (and also PowerShell) that prompts for the "base" of a query, the LDAP syntax filter, and a comma delimited list of attribute values to output linked here:

    Use the optional /csv parameter to output in comma delimited format (instead of the default table format). The output can be redirected to a text (or csv) file. The PowerShell version is also in the script gallery here:

    Richard Mueller - MVP Directory Services

    • ทำเครื่องหมายเป็นคำตอบโดย Misha Rudiy 15 มิถุนายน 2555 16:42
    14 มิถุนายน 2555 18:09