Solved

Script to query user accounts in AD

Posted on 2014-04-02
11
505 Views
Last Modified: 2014-04-03
I need a script that will read a list of user accounts in a txt file and it will export all of the users information from the AD account (general tab) into a file per line for each account.  Prefer a csv file.
0
Comment
Question by:mystikal1000
  • 4
  • 2
  • 2
  • +3
11 Comments
 
LVL 29

Expert Comment

by:becraig
ID: 39972355
import-module ac* 
Get-ADUser -Filter * | export-csv c:\filename.csv

Open in new window

0
 
LVL 19

Expert Comment

by:Kash
ID: 39972368
import-module *activedir*

get-aduser -filter * -SearchBase "DC=mydomain, DC=local" | format-table name,surname | out-file exported.csv
0
 
LVL 1

Author Comment

by:mystikal1000
ID: 39972482
Where does it read a list of user accounts from these powershell commands??
0
 
LVL 29

Expert Comment

by:becraig
ID: 39972504
Get-aduser looks fit for active directory users.

Filter * says all users
0
 
LVL 1

Author Comment

by:mystikal1000
ID: 39972541
I know, but I want it to read certain AD accounts :)
0
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 52

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 39972843
Here's a simple example of doing it with a BAT script that reads the users from the test file one at a time, queries AD using the powerful (and free) ADFIND utility (link below), and outputs whatever fields you desire to a single CSV file.

http://www.joeware.net/freetools/tools/adfind/

@echo off

set UserList=users.txt
set OutFile=adinfo.csv

(
  for /f "usebackq" %%A in ("%UserList%") do (
    adfind -csv -f "(sAMAccountName=%%A)" cn sn name givenname displayname mail telephoneNumber title departmentcode sAMAccountName
  )
) > "%OutFile%"

Open in new window

0
 
LVL 1

Author Comment

by:mystikal1000
ID: 39973430
Awesome Billprew!!!
0
 
LVL 2

Expert Comment

by:MilesLogan
ID: 39973928
You could also do something like ..

gc users.txt | Get-ADUser -Properties * | select GivenName,Initials,Surname,DisplayName,Description,Office,telephoneNumber,mail,HomePage | export-csv data.csv -nti
0
 
LVL 17

Expert Comment

by:Premkumar Yogeswaran
ID: 39974718
Hi,

Please use the below powershell script

You need to provide the input as users.csv

Header should be User example below (samid )

User
ravia
premk
shyamk


import-module activedirectory
import-csv c:\temp\users.csv | % { Get-ADUser $_.user -properties * | export-csv c:\filename.csv -notype }

Open in new window

0
 
LVL 1

Author Comment

by:mystikal1000
ID: 39974821
Didn't work...
0
 
LVL 52

Expert Comment

by:Bill Prew
ID: 39975029
Thanks, glad that was useful.

~bp
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Find out how to use Active Directory data for email signature management in Microsoft Exchange and Office 365.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…

947 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

23 Experts available now in Live!

Get 1:1 Help Now