aben
aben

Reputation: 13

Powershell Pass a csv of names, to LDAP filter by CN

$csv = Get-Content "ListOfNames.csv"

foreach($item in $csv){
Get-ADUser -LDAPFilter '(cn=$item)' -Properties * | Ft sAMAccountName, givenName, emailAddress
}

I'm new to powershell, I'm trying to enter a list of users only by names by their cn parameter
I don't get any result, but when I enter a name instead of $item it works great... What am I missing here ? can't I enter an array in the -LDAPFilter?

Thanks in advance

Upvotes: 0

Views: 259

Answers (2)

Doug Maurer
Doug Maurer

Reputation: 8868

You need to use double quotes in this case for the variable to be able to expand

$csv = Get-Content "ListOfNames.csv"

foreach($item in $csv){
    Get-ADUser -LDAPFilter "(cn=$item)" -Properties emailaddress |
        Format-Table sAMAccountName, givenName, emailAddress
}

Also note that emailaddress is the only property from your desired output not returned by default. Try to avoid -Properties * unless you need all those properties.

Upvotes: 1

Merkle Groot
Merkle Groot

Reputation: 906

It looks like a string formatting issue to me.

Try this:

$csv = Get-Content "ListOfNames.csv"

foreach ($item in $csv) {
  $filter = [string]::Format("'(cn={0})'", $item)
  Get-ADUser -LDAPFilter $filter -Properties * | Ft sAMAccountName, givenName, emailAddress
}

Upvotes: 0

Related Questions