• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 233
  • Last Modified:

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.
0
nav2567
Asked:
nav2567
  • 4
  • 4
  • 3
1 Solution
 
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
 
NVITCommented:
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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
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
 
NVITCommented:
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
 
NVITCommented:
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
 
NVITCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

On-Demand: Securing Your Wi-Fi for Summer Travel

Traveling this summer?Check out our on-demand webinar to learn about the importance of Wi-Fi security and 3 easy measures you can start taking immediately to protect your private data while using public Wi-Fi. Follow us today to learn more!

  • 4
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now