Error message - fetch info about email address

It gets the information I request, but I get an ugly error message for every email address
Anyone who know whats wrong so I can remove that error message.

$txt_location = "Emaillist.txt"
$csv_location = "Emaillist-test.csv"

#Fetch from AD

Get-Content $txt_location | forEach {Get-ADUser -Filter {EmailAddress -eq $_ } -Properties msRTCSIP-PrimaryUserAddress, EmailAddress, Manager | Select-Object msRTCSIP-PrimaryUserAddress, EmailAddress,

@{name='ManagerFirstname';expression={(Get-ADUser -Identity $_.manager | Select-Object -ExpandProperty Givenname)}},

@{name='ManagerLastName';expression={(Get-ADUser -Identity $_.manager | Select-Object -ExpandProperty Surname)}},

@{name='ManagerUserID';expression={(Get-ADUser -Identity $_.manager | Select-Object -ExpandProperty SamAccountName)}},

@{name='ManagerEmailAddress';expression={(Get-ADUser -Identity $_.manager -Properties emailaddress | Select-Object -ExpandProperty emailaddress)}}} | Format-Table -AutoSize | Out-File $csv_location

Error message:

Get-ADUser : The search filter cannot be recognized

At C:\Test\Get-User_byEmail-tes1.ps1:16 char:52

+ ...  | forEach {Get-ADUser -Filter {EmailAddress -eq $_ } -Properties msR ...

+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [Get-ADUser], ADException

    + FullyQualifiedErrorId : ActiveDirectoryServer:8254,Microsoft.ActiveDirectory.Management.Commands.GetADUser

Could you please edit your question and format your code as code? Just like you did with the error message. :wink:

Thanks in advance.

1 Like

Sorry missed that, I have updated it now.

1 Like

Really the -Filter parameter takes a string. A LOT of Microsoft documentation shows using { } which can work, but only because it’s converted to a string behind the scenes. Try this

Get-ADUser -Filter "EmailAddress -eq '$_'"

… agreed with Doug


You should not use something like this:

...| Format-Table -AutoSize | Out-File $csv_location

Format cmdlets should always be the very last in the pipeline!! :wink:

The PowerShell product team spend some effort to provide you with a professional solution for this task. It’s name

Please read the help completely including the examples to learn how to use it.

Thanks in advance.


Completely overlooked that, very good call.