Solved

Script to query user accounts in AD

Posted on 2014-04-02
11
503 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 28

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 28

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
 
LVL 51

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 51

Expert Comment

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

~bp
0

Join & Write a Comment

I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
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…

762 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