Retrieve SamAccountNames from Display Names

This topic contains 8 replies, has 3 voices, and was last updated by  Don Jones 4 years ago.

  • Author
    Posts
  • #9077

    thickgit
    Participant

    Hi All,

    I have a csv list of about 130 Active Directory 'Display Names' which are the users' first names and last names.

    I would like to retrieve the user SamAccountNames from this info. I've tried Import-Csv with a 'foreach' but I'm not having much luck.

    Can anyone help?

    Thanks.

  • #9085

    Charles Downing
    Participant

    What are you using to try and get the SamAccountName attribute? You should be able to use Import-Csv to get the list of display names from your file, and then you can use Get-ADUser with the -filter parameter to pull the data from AD.

    Assuming the display name column of the CSV is DisplayName and that column contains the actual display names from AD:
    $names = Import-Csv c:\temp\displaynames.txt
    foreach ($name in $names) {
    $user = Get-ADUser -filter { Name -like $name.DisplayName }
    $user.SamAccountName
    }

  • #9087

    thickgit
    Participant

    Yes, I was using Import-Csv C:\Displaynames.csv | foreach {Get-ADUser -identity $_.'DisplayName'}

    It imports the CSV fine, but then it just kept telling me:

    Get-ADUser : Cannot find an object with identity: 'Joe Bloggs' under: 'DC=company,DC=pri'

    I've put -f after the ADUser and that didn't work either.

    So I tried your suggestion and I get:

    Get-ADUser : Property: 'DisplayName' not found in object of type: 'System.Management.Automation.PSCustomObject'

  • #9090

    thickgit
    Participant

    Or to put the question differently...

    How can I retrieve a user AD account by providing just the 'Display Name'?

    For a single user, I do:

    Get-ADUser -f * -Properties DisplayName | Where-Object {$_.DisplayName -eq 'Joe Bloggs'}

    I just need to know how to do it from a CSV or text file for multiple users!

  • #9091

    Charles Downing
    Participant

    Does your CSV have a column header row with a DisplayName column?

    C:\Displaynames.csv:
    DisplayName,Department,Cubicle
    Dr. Posh Scripto,IT,BE301

  • #9093

    thickgit
    Participant

    Yes. I've got the 'DisplayName' header and can import it using Import-CSV.

  • #9094

    Charles Downing
    Participant

    Weird...

    Can I see an example of your CSV file, with the header, and then see what happens at the console when you run my code snippet? Also, if you run "$names = Import-Csv C:\Displaynames.csv", then running "$names" should show you the imported names and the column headers that it's using. Can you show me an example of that output?

  • #9095

    thickgit
    Participant

    Yes, if I run '$names' then I get the list of imported names.

    Error message when I run code snippet:

    Get-ADUser : Property: 'DisplayName' not found in object of type: 'System.Management.Automation.PSCustomObject'.
    At line:2 char:19
    + $user = Get-ADUser <<<< -filter { Name -like $name.DisplayName } + CategoryInfo : InvalidArgument: (:) [Get-ADUser], ArgumentException + FullyQualifiedErrorId : Property: 'DisplayName' not found in object of type: 'System.Management.Automation.PSCustomObject'.,Microsoft.ActiveDirectory.Management.Commands.GetADUser

  • #9120

    Don Jones
    Keymaster

    -Identity doesn't accept DisplayName. Use -filter.

    Get-ADUser -filter { DisplayName -eq 'Joe Admin' } | Select samAccountName

    Could be used inside a ForEach to take values from your CSV. -Identity only takes samAccountName, CN, and a couple of other specific things that are indexed in the database.

You must be logged in to reply to this topic.