We help IT Professionals succeed at work.

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.
Comment
Watch Question

Senior Solution Architect
Most Valuable Expert 2015
Top Expert 2015
Commented:
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.
NVITEnd-user support

Commented:
You .bat code works for me. Do you mean c:\names.txt only has the last one? Try changing > to >>

Author

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
.....

Author

Commented:
NewVillageIT, for some reason, my script only displays the last SAMID's name.

Author

Commented:
Will, can you please modify your script to display just the following:

samid           Names
jsmith           Smith, John
mbrown       brown, Mary
........
NVITEnd-user support

Commented:
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

Author

Commented:
NewVillageIT, the names.txt file is blank after running your script.
NVITEnd-user support

Commented:
Nav..  What happens to names.txt if you remove:

| find ","
Will SzymkowskiSenior Solution Architect
Most Valuable Expert 2015
Top Expert 2015

Commented:
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.
NVITEnd-user support

Commented:
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

Will SzymkowskiSenior Solution Architect
Most Valuable Expert 2015
Top Expert 2015

Commented:
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.