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 Active Directory users with which looks like this:


The code looks like this:

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

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