?
Solved

Powershell get samaccountname from display name

Posted on 2011-10-06
8
Medium Priority
?
4,743 Views
Last Modified: 2012-05-12
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.
0
Comment
Question by:arsenal22
  • 4
  • 3
8 Comments
 
LVL 24

Accepted Solution

by:
yo_bee earned 2000 total points
ID: 36928022
what about using CVSDE?
This will give you a text file to work with.


csvde -d "DC=Domain,DC=local" -m -f c:\outputtest.csv -l name,sAMAccountName -r "(&(objectCategory=user)(objectClass=user)(cn=*)(displayName=*)(userPrincipalName=*))"

Modify the Displayname=* to a value that meets your needs
0
 
LVL 27

Expert Comment

by:KenMcF
ID: 36930213
Do you want a list of all users listing their displayname and samaccountname or d you want to be able to search for a user by the displayname and get the samaccountname?


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
0
 

Author Comment

by:arsenal22
ID: 36931070
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
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.

 
LVL 27

Expert Comment

by:KenMcF
ID: 36931135
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
0
 

Author Comment

by:arsenal22
ID: 36932285
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
0
 
LVL 27

Expert Comment

by:KenMcF
ID: 36932315
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,DC=Local" | Select DisplayName
0
 

Author Comment

by:arsenal22
ID: 36932485
I have the display name in the text file and i want to have the samaccountname populated for me.
0
 
LVL 27

Expert Comment

by:KenMcF
ID: 36932511
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.

0

Featured Post

Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Compliance and data security require steps be taken to prevent unauthorized users from copying data.  Here's one method to prevent data theft via USB drives (and writable optical media).
Active Directory can easily get cluttered with unused service, user and computer accounts. In this article, I will show you the way I like to implement ADCleanup..
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…

750 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question