Get real name from ad account logon name

Posted on 2011-05-10
Last Modified: 2012-05-11

Hope someone can help me please. I am looking for a powershell script that will take a txt file containg AD usernames and output next to the usernames the real names from the AD accounts of the user's in question. e.g.

Username         Real name
usera                John Smith
userb                Davey Jones

Is this possible in powershell? or does this need to be done in vbs.

Hope someone can help out.


Question by:karlpearson
    LVL 3

    Expert Comment

    LVL 70

    Accepted Solution

    PowerShell can do it, there's a variety of different ways.

    Quest CmdLets:
    Get-Content YourFile.txt | Get-QADUser | Select-Object Name, SamAccountName

    Open in new window

    Exchange CmdLets:
    Get-Content YourFile.txt | Get-Mailbox | Select-Object Name, SamAccountName

    Open in new window

    AD CmdLets:
    Get-Content YourFile.txt | Get-ADUser | Select-Object Name, SamAccountName

    Open in new window

    Native (no CmdLets - PowerShell 2):
    Get-Content YourFile.txt | ForEach-Object {
    } | Select-Object @{n='Name';e={ $_.Properties["name"][0] }}, @{n='SamAccountName';e={ $_.Properties["samaccountname"][0] }}

    Open in new window

    In all cases you can write to a file by adding a  pipe into "Export-Csv" on the end.


    LVL 13

    Expert Comment

    This solution uses Quest's AD snapin and outputs the result into Users2.TXT file. Customize the paths in lines 1 and 6.
    $users = Get-Content c:\ee\users.txt
    Add-PSSnapin Quest.ActiveRoles.ADManagement -ErrorAction silentlycontinue
    $users | ForEach-Object {
        Get-QADUser -SamAccountName $_ | Select-Object @{n="Username";e={$_.samaccountname}}, @{n="Real name"; e={$_.displayname}}
    } | convertto-Csv -NoTypeInformation -Delimiter "`t" | %{$_ -replace '"',""} | Set-Content -Path C:\ee\users2.txt -Encoding unicode

    Open in new window


    Author Closing Comment

    Great stuff thank you

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Suggested Solutions

    Title # Comments Views Activity
    countHi2 challenge 7 30
    c# PowerShell runspace woes 5 37
    word0 challenge 4 34
    wordlen challenge 3 26
    How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
    "Migrate" an SMTP relay receive connector to a new server using info from an old server.
    The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
    The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now