Not Getting Error Variable Name

Welcome Forums General PowerShell Q&A Not Getting Error Variable Name

This topic contains 3 replies, has 3 voices, and was last updated by

2 years, 12 months ago.

  • Author
  • #34105

    Points: 0
    Rank: Member

    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" }

  • #34106

    Points: 5
    Rank: Member


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

  • #34107

    Points: 148
    Rank: Participant

    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=$ 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:

  • #34108

    Points: 0
    Rank: Member

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

The topic ‘Not Getting Error Variable Name’ is closed to new replies.