Not Getting Error Variable Name

Hi Guys,

I am trying to fetch the user details via below script, When I run it without exporting to csv, I see there are some users id which cant be found although not the email id’s I can see, so tried to use try catch loop, but still it only shows not found not the email address. Could you please advise ?

$users=Get-Content ‘xxxxxxxx:h\Desktop\user.txt’

foreach ($u in $users)

{

try {

Get-ADUser -Filter {Emailaddress -eq $u} -Properties * |select Emailaddress, @{n=‘User Location’;e=‘co’} |Export-Csv xxxxxxxx\user.csv -Append -NoTypeInformation

}

catch { Write-Output “Notfound $u” }
}

Partho,

One possibility is that you have a blank line in the text file.

I think Tim’s possibility is valid. Gut your function momentarily, and just put Write-Output $U. This will allow you to first determine that you iterating thought the file’s contents properly. It’s after this, that you start adding the try-catch blocks and Get-ADUser cmdlets.

Another concern, for me at least, is your calculated property – that’s busted. One, you need to reference the property in the Expression portion with $, or in PowerShell 3.0 and greater, as $PSItem. …E=$.co… Second, and quite important, is that there isn’t a “co” property. Run this:

Get-ADUser -Identity auser -Properties *

and look though the properties (replacing auser with a valid SamAccountName). You’ll see that there’s no property called “co.” Replace it with something that actually exists. Did you mean Country? If so, your calculated property would be:

@{N='Location';E={$_.Country}}

Oh man Tim, You are absolutely right, I dint noticed that as the text file contains more than 2000 lines. Thanks dude :slight_smile: