nav2567
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.
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.
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?
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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.
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.
$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
}
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.
For these what do you want (home/business/fax)
Address
City
State
ZIP or equivalent
Phone