0
votes

I have a list of email addresses in a CSV file from which I would like to get the employee number against each row exported into a new CSV file. I tried with a powershell script but am struggling a bit and was wondering if somebody could point me in the right direction.

Import-Module ActiveDirectory
Import-Csv 'C:\ps\EmailIDIFS.csv' | ForEach {
Get-ADUser -Filter "EmailAddress -eq '$($_email)'" -Properties Name, 
emailAddress, SAMAccountName, employeeNumber | `
Select Name, emailAddress, SamAccountName, employeeNumber | `
Export-CSV 'C:\ps\ADResults.csv' -NoTypeInformation
}

Unfortunately when I run this I get the following error:

Get-ADUser : The search filter cannot be recognized At C:\Users\jfoote\Nextcloud\Local\scripts\ADEmployeeNumber.ps1:3 char:1 + Get-ADUser -Filter "EmailAddress -eq '$($_email)'" -Properties Name, ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Get-ADUser], ADException + FullyQualifiedErrorId : ActiveDirectoryServer:8254,Microsoft.ActiveDirectory.Management.Commands.GetADUser

1

1 Answers

1
votes

It looks like you're just missing a period on this line:

Get-ADUser -Filter "EmailAddress -eq '$($_email)'" -Properties Name, emailAddress, SAMAccountName, employeeNumber

The $_email should probably be $_.email (assuming you have a heading called "email" in your CSV file).

Note that an email address can be used in the Identity parameter as well, which is the default parameter. So you could simplify it like this:

Get-ADUser $_.email -Properties Name, emailAddress, SAMAccountName, employeeNumber

The effect is exactly the same. It's just easier to read.