0
votes

I am trying to get the AD user account via Powershell, I need to import the name from csv and retrieve their AD results.

The list is only stored with "Display Name"

test.csv

name
Peter Chan
John Wu
Tom Wong

PS script

 $list = Import-Csv '.\test.csv'

foreach ($i in $list) {
    Get-ADUser -Filter "Name -eq '$i.name'"
}

Error

Get-ADUser : Error parsing query: 'Name -eq @{name=Peter Chan}.name' Error Message: 'syntax error' at position: '10'.
At line:2 char:1
+ Get-ADUser -Filter "Name -eq $i.name"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ParserError: (:) [Get-ADUser], ADFilterParsingException
    + FullyQualifiedErrorId : Error parsing query: 'Name -eq @{name=Peter Chan}.name' Error Message: 'syntax error' at
   position: '10'.,Microsoft.ActiveDirectory.Management.Commands.GetADUser

When I run Get-ADUser -Filter "Name -like 'Peter Chan'" , I can get the result i want. It shows that it is a array type @{name=Peter Chan}.name, what should I need to change on the code?

1

1 Answers

2
votes

Variables get expanded in strings not property expressions.

Change this:

Get-ADUser -Filter "Name -eq '$i.Name'"

TO

Get-ADUser -Filter "Name -eq '$($i.Name)'"