Covert User Email Address (UPN) to SAMACCOUNTNAME With PowerShell

In this blog post, I will show you how to convert a list of Active Directory users email address or UPN to their SAMACCOUNTNAME.

File

The data file can be a TXT file with a list of all the email addresses with the following format.

emailaddress@domain .local
emailaddress2@domain .local

Save the as upn.txt

Code

The code below is using the Active Directory module which is available on any machine that the AD administrative tools or on a Domain Controller.

Get-Content .\upn.txt | Foreach-Object{ Get-ADUser -Filter {UserPrincipalName -eq $_}}| Select SamAccountName | export-csv sam.csv

After running the code, the output will be in the form of a CSV file.


Posted

in

,

by