Powershell : Convert a list of samAccountNames to UPN/Mail from ADDS

Yes, another mission, where people like to use SamAccountName names sometimes which is a pain when you need the UPN names, the difference is this:

SamAccountName : LCroucher2
UPN : lee6e2d4a@croucher.cloud
Mail Address: lee.croucher@croucher.cloud

The UPN is how you should be logging into servers and other devices, the SamAccountName is the pre-Windows 2000 login name that is limited to 15 characters for NETBIOS reasons  (well, its actually 16 but the last character is reserved in Windows) so after this limit it will be truncated and some companys will add numbers of letters onto the end.

Note : Your UPN does not need to be the same as your email address, but in many companies it will be for ease of administration and support, if anything that is discouraged as if people know your e-mail they also know your UPN in this environment.

Powershell : samAccountNametoMail.ps1

This script will lookup the "mail" attribute, if indeed you do have you UPN the same as you e-mail address you can use the script further down this post!

# Import the Active Directory module
Import-Module ActiveDirectory

# Path to the file containing samAccountNames
$samAccountFile = "userlist.txt"

# Read the samAccountNames from the file
$samAccountNames = Get-Content -Path $samAccountFile

# Create an empty list to store the email addresses
$emailList = @()

# Iterate over each samAccountName
foreach ($samAccountName in $samAccountNames) {
    # Search for the user in Active Directory
    $user = Get-ADUser -Filter { SamAccountName -eq $samAccountName } -Property mail   
    # Check if the user was found
    if ($user) {
        # Retrieve the mail attribute and add it to the list
        $emailList += $user.mail
    } else {
        # Handle the case where the user is not found
        $emailList += "Not Found"
    }
}

# Output the email addresses to the console
$emailList | ForEach-Object { Write-Output $_ }

# Save the email addresses to a new file
$emailList | Out-File -FilePath "emailList.txt"

Powershell : samAccountNamestoUPN.ps1

This example assumes you UPN is the same as your e-mail address, so, if you are given a list of SamAccountName values and you need to get the e-mail address from them then you can run this script to accomplish that:

# Import the Active Directory module
Import-Module ActiveDirectory

# Path to the file containing samAccountNames
$samAccountFile = "userlist.txt"

# Read the samAccountNames from the file
$samAccountNames = Get-Content -Path $samAccountFile

# Create an empty list to store the UPNs
$upnList = @()

# Iterate over each samAccountName
foreach ($samAccountName in $samAccountNames) {
    # Search for the user in Active Directory
    $user = Get-ADUser -Filter { SamAccountName -eq $samAccountName } -Property UserPrincipalName   
    # Check if the user was found
    if ($user) {
        # Retrieve the UPN and add it to the list
        $upnList += $user.UserPrincipalName
    } else {
        # Handle the case where the user is not found
        $upnList += "Not Found"
    }
}

# Output the UPNs to the console
$upnList | ForEach-Object { Write-Output $_ }

# Save the UPNs to a new file
$upnList | Out-File -FilePath "upnlist.txt"


Previous Post Next Post

نموذج الاتصال