looking up ad user by email from text file

Welcome Forums General PowerShell Q&A looking up ad user by email from text file

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

 
Participant
1 year, 11 months ago.

  • Author
    Posts
  • #61615

    Participant
    Points: 53
    Rank: Member

    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
    Points: 0
    Rank: Member

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

  • #61623

    Participant
    Points: 251
    Helping Hand
    Rank: Contributor

    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

      Participant
      Points: 53
      Rank: Member

      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

    Participant
    Points: 251
    Helping Hand
    Rank: Contributor

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

  • #61647

    Participant
    Points: 251
    Helping Hand
    Rank: Contributor

    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

      Participant
      Points: 53
      Rank: Member

      same as before, doesn't display $Get

    • #61654

      Participant
      Points: 251
      Helping Hand
      Rank: Contributor

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

  • #61657

    Participant
    Points: 53
    Rank: Member

    found the problem, I had some "white spaces"

  • #61672

    Participant
    Points: 0
    Rank: Member

    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

The topic ‘looking up ad user by email from text file’ is closed to new replies.