looking up ad user by email from text file

Tagged: 

This topic contains 10 replies, has 4 voices, and was last updated by  Richard Siddaway 7 months, 1 week ago.

  • Author
    Posts
  • #61615

    Tony Antony
    Participant

    I have a text file with some email addresses

    $text = Get-Content 'C:\Users\tantony\Desktop\email.txt'
    
    foreach ($emails in $text)
    {
        $Get = Get-ADUser -Filter {Emailaddress -like "$emails"}    
    }

    But, when I run this, it's not displaying anything. The emails I have in the list are valid exchange emails.

    Thanks,

  • #61621

    Ron
    Participant

    The code you posted doesn't output anything. You are assigning the results to a variable, but you never display that variable.

  • #61623

    Olaf Soyk
    Participant

    You should at least have the last user in your variable $Get. 😉
    Try this:

    $text = Get-Content 'C:\Users\tantony\Desktop\email.txt'
    
    foreach ($emails in $text)
    {
        Get-ADUser -Filter {Emailaddress -like "$emails"} -Properties Emailaddress   
    }
    • #61630

      Tony Antony
      Participant

      Thank you,

      I tried that, but it still doesn't display anything. It should be displaying $Get, but it's not.

      $text = Get-Content 'C:\Users\tantony\Desktop\email.txt'

      foreach ($emails in $text)
      {
          $Get = Get-ADUser -Filter {Emailaddress -like "$emails"} -Properties Emailaddress     
          $Get  
      }
  • #61635

    Olaf Soyk
    Participant

    So your text file might be the problem. Can you post a few lines of it here? But you should obfuscate sensitive information.

  • #61647

    Olaf Soyk
    Participant

    OK, try this:

    $text = Get-Content 'C:\Users\tantony\Desktop\email.txt'
    foreach ($emails in $text)
    {
        $Get = Get-ADUser -Filter {Emailaddress -like $emails} -Properties Emailaddress     
        $Get  
    }
    • #61650

      Tony Antony
      Participant

      same as before, doesn't display $Get

    • #61654

      Olaf Soyk
      Participant

      hmmm ... I tried it and it's been working for me.

  • #61657

    Tony Antony
    Participant

    found the problem, I had some "white spaces"

  • #61672

    Richard Siddaway
    Moderator

    Don't need to do this
    $Get = Get-ADUser -Filter {Emailaddress -like $emails} -Properties Emailaddress
    $Get

    as
        Get-ADUser -Filter {Emailaddress -like $emails} -Properties Emailaddress
        
    will display the result

You must be logged in to reply to this topic.