script to read a txt file of SAMID and display full user names.

Hello,

I need a command to read a txt file which contains a list of SAMID and out the full user names into another txt file.  

I have tried the following but it only displays the full user name of the last SAMID:

for /f %%i in (c:\samid.txt) do dsquery user -samid %%i |dsget user -fn -ln > c:\names.txt

Can someone advise what is missing?

Thanks.
nav2567Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Will SzymkowskiSenior Solution ArchitectCommented:
You can do this via powershell. See command below...
import-module activedirectory
$UserList = get-content "c:\filename.txt"
ForEach ($User in $UserList) {
Get-ADUser -Identity $User | select Name | Out-file "c:\filename2.txt" -append
}

Open in new window


Will.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
NVITEnd-user supportCommented:
You .bat code works for me. Do you mean c:\names.txt only has the last one? Try changing > to >>
0
nav2567Author Commented:
Fine.  I am getting the following display:

Name
---------------
Smith, John

Name
-------------
Brown, Mary

Can you modify the script to get rid of the repeated titles?

Name
-----------
Smith, John
Brown, Mary
.....
0
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

nav2567Author Commented:
NewVillageIT, for some reason, my script only displays the last SAMID's name.
0
nav2567Author Commented:
Will, can you please modify your script to display just the following:

samid           Names
jsmith           Smith, John
mbrown       brown, Mary
........
0
NVITEnd-user supportCommented:
Maybe this...
for /f %%i in (c:\samid.txt) do dsquery user -samid %%i | dsget user -fn -ln | find "," >> c:\names.txt

Open in new window

0
nav2567Author Commented:
NewVillageIT, the names.txt file is blank after running your script.
0
NVITEnd-user supportCommented:
Nav..  What happens to names.txt if you remove:

| find ","
0
Will SzymkowskiSenior Solution ArchitectCommented:
import-module activedirectory
$UserList = get-content "c:\filename.txt"
ForEach ($User in $UserList) {
Get-ADUser -Identity $User | select samaccountname, Name | Out-file "c:\filename2.txt" -append
}

Open in new window


That does it.

Will.
0
NVITEnd-user supportCommented:
for /f %%i in (c:\samid.txt) do dsquery user -samid %%i | dsget user -fn -ln | find /v "fn" >> c:\names.txt

Open in new window

0
Will SzymkowskiSenior Solution ArchitectCommented:
Are you going to close this question? I have provided you the additional code required as requested. If you are having issues or is it not working as expected, please let me know.

Will.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Active Directory

From novice to tech pro — start learning today.