Retrieve SamAccountNames from Display Names

This topic contains 10 replies, has 5 voices, and was last updated by  Jon 5 months ago.

  • Author
  • #9077


    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?


  • #9085

    Charles Downing

    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 }

  • #9087


    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


    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

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

    Dr. Posh Scripto,IT,BE301

  • #9093


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

  • #9094

    Charles Downing


    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


    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

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

  • #91588

    Reginald Cabine

    Thanks Don, I was looking for this exact cmdlet but couldn't remember the format.

  • #91591

    gc C:\scripts\displaynames.txt | % {Get-ADUser -LDAPfilter "(DisplayName=$_)"} | select samaccountname

You must be logged in to reply to this topic.