scripter78
scripter78

Reputation: 1177

Powershell loop through CSV Compare Email to ActiveDirectory

If I run this script without the look and manually enter say my email for the section of ([email protected]) it works and says User found in AD but when I run this script to loop through it comes back with the full list of email addresses with User does not exist in AD preceding the email address as if it cannot find the email address in AD.

What is it that I am doing wrong here?

$path = "H:\users.csv"
$csv = Import-Csv $path 
Import-Module ActiveDirectory

foreach($line in $csv)
{
    $User = Get-ADUser -LDAPFilter "(&(objectclass=user)(mail=$line.Email))"
    If ($User -eq $Null) {"User does not exist in AD   " + $line.Email }
    Else {"User found in AD  - " + $line.Email}
}

Upvotes: 1

Views: 3239

Answers (1)

Frode F.
Frode F.

Reputation: 54911

You forgot to use subexpression $() when accessing the email property in our ldap-filter. Try this:

$User = Get-ADUser -LDAPFilter "(&(objectclass=user)(mail=$($line.Email)))"

Subexpressions are required when your accessing a more custom value then a pure string variable(like a property of an object).

Upvotes: 2

Related Questions