Get-Aduser -Filter no output


My csv file is…

Firstname lastname
Firstname lastname
Firstname lastname

My Code is this

$list = Import-Csv c:\pathtocsv.csv
ForEach($user in $list){    
    $dn = $user.user
    Get-ADUser -Filter { displayName -like $dn } -Properties *| Select samaccountname,displayname,emailaddress|Export-Csv C:\scripts\export2.csv -NoType -Append } 

I get no output from the Get-ADUser. However if check the value of $dn, then i get a “Firstname lastname” and if replace $df with a name then i do get an output. I am so confused why?

Hi, welcome to the forum :wave:

That looks like it should work, but only for an exact match, because you’ve not used any wildcards in your filter. Do your display names in AD match your CSV data exactly?

PS E:\Temp> $dn = 'John Smith'
PS E:\Temp> 'John Smith' -like $dn
PS E:\Temp> 'Smith, John' -like $dn
PS E:\Temp> 'John Smith - Accounts' -like $dn

For the love of god i’ve been banging my head against a wall for some time now looking at the powershell code! It was right there infront of my eyes the hole time. the information in the CSV was not in the right order!

Thanks a bunch matt! :slight_smile: