Search code examples
powershell-3.0

Get-ADUser with display name as a value


**I have list of users display name in CSV file and I am trying to get samAccountName and export it to CSV file but its not working, I understand that get-aduser doesnt accept display name as a value so I used filter but still not work help please:)

CSV file format

User

Amer, john
Doe, John
smith, john
**

$list = Import-Csv C:\export.csv
foreach ($user in $list) {
Get-ADUser -filter { DisplayName -eq "user.user" } | Select samAccountName | Export-csv C:\export1.csv
}

Solution

  • Try

    ForEach($user in $list{    
    $dn = $user.user
    Get-ADUser -Filter { displayName -like $dn } | Select samAccountName > C:\export1.csv}
    

    Also verify your Display names from AD match what is in CSV. But this worked for me. At first I couldn't export to C directly so I exported CSV to C:\AD\export.csv