Solved

create a PS script to read txt file of names and list AD fields.

Posted on 2014-12-05
7
124 Views
Last Modified: 2014-12-24
Hi,

Can someone advise how to create a PS script to read a txt file of names and list the following fields in AD:

Login Name
First Name
Last Name
OU        
Email address
Address
City                      
State
ZIP or equivalent
Phone Number

Also, in the txt file, should I put the DN of the users or should I use SAMIDs?

Please advise.

Thanks.
0
Comment
Question by:nav2567
7 Comments
 
LVL 79

Expert Comment

by:David Johnson, CD, MVP
ID: 40484232
use samaccountname
For these what do you want (home/business/fax)
Address
City                      
State
ZIP or equivalent
Phone
0
 

Author Comment

by:nav2567
ID: 40485103
Thanks.

I used to use the command ldifde long time ago but I would like to try a Powershell script to achieve the same result.

Can you please write a simple sample script so I can reference to get started?
0
 
LVL 39

Accepted Solution

by:
footech earned 500 total points
ID: 40485253
You can use either the samAccountName or DistinguishedName in the text file.  I'll leave it up to you to decide which is easier.  There's a default set of properties that are retrieved by Get-ADUser.  If you want others you have to specify them.  Since OU isn't an attribute of an AD object, you have to use a calculated property (i.e. I'm calculating the OU from the DistinguishedName).
Get-Content file.txt |
 Get-ADUser -Properties Mail,StreetAddress,City,State,postalCode,telephoneNumber |
 Select samAccountName,GivenName,Surname,@{n="OU";e={($_.DistinguishedName -split ",",2)[-1]}},Mail,StreetAddress,City,State,postalCode,telephoneNumber

Open in new window

0
Is Your AD Toolbox Looking More Like a Toybox?

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 21

Expert Comment

by:dan_blagut
ID: 40485270
Hello
Here you can find a good approach and all fields
https://gallery.technet.microsoft.com/scriptcenter/Getting-Users-ALL-7417b71d

in the command line you should list all fields and use -AutoSize and  -Width paramteres to see all data.
get-content c:\users.txt | get-aduser -pr SamAccountName,FirstName, sn....|out-file .\output.txt

Anyway, the OU field can't be extracted (you can calculated this field from CN later)

Dan
0
 
LVL 24

Expert Comment

by:Coralon
ID: 40485911
You can do this without a special module by leveraging .Net.
Using the samaccountname is definitely the easiest way to go, since they are guaranteed to be unique.
$Users = get-content -path c:\temp\users.txt
$SearchRoot = New-Object -TypeName System.DirectoryServices.DirectoryEntry("LDAP://OU=users,dc=domain,dc=com"

$Users | foreach-object {
      $user = $_
      $ADSI = [adsisearcher]"(objectCategory=user)(samaccountname=$user)"
      $ADSI.SearchRoot = $SearchRoot
      $UserObject = $ADSI.FindOne()
      $Properties = $UserObject | select-object -property samaccountname,givenName,sn,dn,mail,streetAddress,city,state,postalCode,telephoneNumber 
      $Properties -join "," | out-file -filepath c:\temp\UserProperties.csv -append
}

Open in new window


This isn't 100% tested, but it should work.  

Coralon
0
 
LVL 39

Expert Comment

by:footech
ID: 40486053
Using the samaccountname is definitely the easiest way to go, since they are guaranteed to be unique.
By that statement I would have to say that you're implying that the distinguishedname isn't unique, which would be false.

But it is a good point about another option besides the AD cmdlets.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

This article will help you understand what HashTables are and how to use them in PowerShell.
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
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 from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …

813 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

11 Experts available now in Live!

Get 1:1 Help Now