none
how do you get samAccountName from displayname? RRS feed

  • Question

  • i have a csv file that contains display names. i need to get the samAccountName of those users. my attempt below doesn't seem to know "-Identity" but when used outside of the script it works:

    foreach ($name in $names) {
        Get-ADUser -Identity $_.$name -Properties samAccountName | Select-Object samAccountName

    that's the partial script where it fails

    "Get-ADUser : Cannot validate argument on parameter 'Identity'. The argument is null.
    
    +     get-aduser -identity $_.$name -Properties samAccountName | Select ...
    +                          ~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Get-ADUser], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.GetADUser"

    same Get-ADUser line works from command line so i can't figure out why it fails.

    Monday, November 14, 2016 8:17 AM

Answers

  • i have a csv file that contains display names

    Get-ADUser Identity parameter doesn't accept DisplayName.
    It accepts DN or GUID or SID or SamAccountName
    You need to use Filter parameter to find the SamaAcountName if you are using displayname

    Import-Csv D:\PSTest\Users.csv | Foreach {
    	Get-ADUser -Filter "DisplayName -eq '$($_.Name)'" | Select SamAccountName
    }
    Get-ADUser

    • Marked as answer by Reno Mardo Monday, November 14, 2016 12:52 PM
    Monday, November 14, 2016 10:02 AM

All replies

  • if you have a foreach loop with the variable $name why don't you use it? 

    Get-ADUser -Identity $_.$name -Properties samAccountName | Select-Object samAccountName

    should be

    Get-ADUser -Identity $name -Properties samAccountName | Select-Object samAccountName

    I think.


    Grüße - Best regards

    PS:> (79,108,97,102|%{[char]$_})-join''

    Monday, November 14, 2016 8:39 AM
  • tried that already but it fails on "-Identity":

    Get-ADUser : Cannot find an object with identity: '@{fname=Mohammed Hussain}'

    Monday, November 14, 2016 8:53 AM
  • i have a csv file that contains display names

    Get-ADUser Identity parameter doesn't accept DisplayName.
    It accepts DN or GUID or SID or SamAccountName
    You need to use Filter parameter to find the SamaAcountName if you are using displayname

    Import-Csv D:\PSTest\Users.csv | Foreach {
    	Get-ADUser -Filter "DisplayName -eq '$($_.Name)'" | Select SamAccountName
    }
    Get-ADUser

    • Marked as answer by Reno Mardo Monday, November 14, 2016 12:52 PM
    Monday, November 14, 2016 10:02 AM
  • if you are looping on $name and the column in your CSV you want to use called "displayname" then it will be

    Get-ADUser -Identity $name.displayname

    Monday, November 14, 2016 10:21 AM
  • did you try Vincents approach?

    Grüße - Best regards

    PS:> (79,108,97,102|%{[char]$_})-join''

    Monday, November 14, 2016 10:21 AM
  • yes! that's it :)

    such an elementary problem. thank you!

    Monday, November 14, 2016 12:53 PM