Solved

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

Posted on 2014-12-05
7
123 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
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

This article will help you understand what HashTables are and how to use them in PowerShell.
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
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…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

863 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

18 Experts available now in Live!

Get 1:1 Help Now