Link to home
Start Free TrialLog in
Avatar of nav2567
nav2567Flag for United States of America

asked on

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

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.
Avatar of David Johnson, CD
David Johnson, CD
Flag of Canada image

use samaccountname
For these what do you want (home/business/fax)
Address
City                      
State
ZIP or equivalent
Phone
Avatar of nav2567

ASKER

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?
ASKER CERTIFIED SOLUTION
Avatar of footech
footech
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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
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.