Powershell to pull SMTP addresses for specific users RRS feed

  • Question

  • Hey all, this is probably an easy one to answer and I am just missing something in the syntax.

    My ultimate goal code is similar:

    Import-CSV Users.csv | ForEach {Get-recipient -filter "SAMAccountName -like '*$_.sAMAccountName'" | Select Name -ExpandProperty EmailAddresses | Select Name, SmtpAddress | Export-csv C:\temp\AllEmailAddress.txt}

    When I run the above, I get nothing back...  So to start troubleshooting it, I created just the below without the loop to test the command itself...

    Get-recipient -filter "SAMAccountName -like '*<username>'" | Select Name -ExpandProperty EmailAddresses | Select Name, SmtpAddress

    When I run it, I get the below:

    So then I take out the last Select statement to just see if there are smtp addresses that should return and I get 3 results as I should...  Below is the test to get some smtp:

    Get-recipient -filter "SAMAccountName -like '*<username>'" | Select Name -ExpandProperty EmailAddresses

    Any ideas are greatly appreciated.  Just substitute <username> with a valid user account from your AD to test the last two code snippets.


    • Edited by Tico M Tuesday, November 3, 2015 11:07 PM
    Tuesday, November 3, 2015 11:06 PM


  • We found our answer with:

    Import-CSV Users.csv | foreach {get-recipient -Identity $_.SamAccountName | select DisplayName, PrimarySmtpaddress} | export-csv "C:\temp\smtpaddress.csv" 


    • Marked as answer by Tico M Wednesday, November 4, 2015 3:32 PM
    Wednesday, November 4, 2015 3:31 PM