PowerShell Code: Get Email Address From SAMACCOUNTNAME

This Active Directory PowerShell Module code will show you how to the primary SMTP address of users using their SAMACCOUNTNAME.

In this scenario, You have a list of users’ SAMACCOUNT name login details and you want to know \ get their email address.

In this case, My Users.txt file has a list of users with which looks like this:

User1
User2
User3

The code looks like:

$x = Get-Content .\users.txt

$x | Foreach {Get-ADUser -Filter {UserPrincipalName -like $_ } -Properties *|Select SAMAccountName, emailaddress} | Export-Csv USERS.csv

The code will export the email address and SAMaccountName of the users to a .CSV file.