arsenal22
asked on
Powershell get samaccountname from display name
How do i create a powershell to read the display name of a user and output to a file on my c drive or desktop the sAmaccountname. ? Once i do that i then need to move them to a new ou but i can already built that script.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you, so basically KenMcF , I currently have a list of 103 users and we are testing our new ou structure and inorder to move these more efficiently. I would like to run a script to read a current collumn in a csv. or excel worksheet and have powershell return the samaccountname for these users either in a txt or another csv file since i dont believe it can return the value into the same doc without a little more work. Please point me in the right direction or a current script you use .
Thanks
Thanks
You can use something like this
$Users = Get-Content c:\temp\users.txt
$Users | Foreach {
get-qaduser -displayname $_ | Select samaccoutname, Displayname
} | export-csv c:\temp\users.csv -notype
$Users = Get-Content c:\temp\users.txt
$Users | Foreach {
get-qaduser -displayname $_ | Select samaccoutname, Displayname
} | export-csv c:\temp\users.csv -notype
ASKER
The script runs, but returns no value in the csv file that it creates any ideas? It needs to read the text. Is there a certain way that i need to write the txt file on how i show the information?
$Users = Get-Content c:\temp\cb.txt
$Users | Foreach {
get-user -displayname $_ | Select samaccoutname, Displayname
} | export-csv c:\temp\users.csv -notype
$Users = Get-Content c:\temp\cb.txt
$Users | Foreach {
get-user -displayname $_ | Select samaccoutname, Displayname
} | export-csv c:\temp\users.csv -notype
The user names in the txt file, do they match the displayname of the users? one way to check is run
get-qaduser -searchroot "OU=Test_Users,DC=Domain,D C=Local" | Select DisplayName
get-qaduser -searchroot "OU=Test_Users,DC=Domain,D
ASKER
I have the display name in the text file and i want to have the samaccountname populated for me.
But in the text file, do they match the output of the command I posted before? if they do not match exactly then this will not work. We will need to filter another way.
Here are a few examples using the Quest AD cmdlets and powershell.
http://www.quest.com/powershell/activeroles-server.aspx
But by using displaynames you may get multiple results from your search if users have the same name.
Get-qaduser -sl 0 | Select Displayname, Samaccountname | Export-csv c:\temp\users.csv -notype
get-qaduser -displayname "Doe, John" | Select samaccoutname | export-csv c:\temp\users.csv -notype