Need help querying specific attributes using DSQUERY and exporting to CSV

Posted on 2011-05-10
Last Modified: 2012-05-11
You would think this is simple, but I can't find my answers anywhere.  I have a spreadsheet with a users display name, employee number, and their user ID for a program we use here at work.  Any time I'm using dsquery to get the required information it is leaving a ton of blank spaces in the CSV, and it's not pulling the "employeeID" attribute I'm trying to get it to pull - just the cn.  That, and when I can manipulate things to make it pull multiple attribute values at once, it is putting all of the information for a single user (also grabbing computers) in a single cell in the CSV which is completely useless to me.  Can somebody help?
Question by:Archie314
    LVL 28

    Accepted Solution

    dsquery * dc=domain_part1,dc=domain_part2 -filter "(&(objectCategory=Person)(objectClass=User))" -attr samaccountname employeeID displayname

    To import it into Excel, use "Import Text file" and select fixed column width.

    LVL 28

    Expert Comment

    To generate a real CSV file, you can run the following script (modify the domain info matching your domain)
    if exist userlist.csv del userlist.csv
    dsquery * dc=domain_part1,dc=domain_part2 -filter "(&(objectCategory=Person)(objectClass=User))" -attr samaccountname employeeID displayname>templist.tmp
    for /f "tokens=1-4" %%a in (templist.tmp) do echo "%%a";"%%b";"%%c %%d">>userlist.csv

    Open in new window


    Author Closing Comment

    Thank you for the assistance.
    LVL 28

    Expert Comment

    Hi, Archie314, why only "B" grade? Whats missing from my solution?

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    This post first appeared at Oracleinaction  ( Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
    NTFS file system has been developed by Microsoft that is widely used by Windows NT operating system and its advanced versions. It is the mostly used over FAT file system as it provides superior features like reliability, security, storage, efficienc…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now