If your Global Address List (GAL) or AD environment is configured for Last Name, First Name; it can be frustrating when trying to script bulk user actions when your datasource is First name first. If you’re just talking about a one time operation it isn’t too difficult to paste the names into Excel columns and sort them out that way. However, if you are trying to create an interactive script or are running some sort of automated process, trying send everything to Excel first is difficult and resource intensive.

There are plenty of articles on-line that suggest various methods of dealing with this issue but when I tried, lots of them just didn’t work. So, I wrote my own. At my day job I publish an internal blog called PowerShell Friday. I use it to share my PowerShell knowledge with my fellow administrators. The code below is from that blog. It isn’t extremely useful on its own, but I’ve used the techniques it demonstrates in countless scripts and applications. Since it was written as a learning exercise; each line is commented. It demonstratrates the following methods:

  • PowerShell Remoting – specifically to Microsoft Exchange Server
  • Custom credentials prompt – Adds text to the default dialog box
  • PowerShell Functions
  • PowerShell text manipulation
#Get-EmailAddress - Conversts Last Name, First Name to AD Logon Account and looks up email addresses.
#Get Exchange Admin Credentials and Connect to Exchange
$UserCredential = Get-Credential -Message "Credentials are required to access Active Directory and Microsoft Exchange, use the detected username or enter a different account." -UserName ($env:userdomain +'\'+ $env:username) #creates a pop up authentication box to get your username and password
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://FQDNOFYOUREXCHANGESERVER/PowerShell/ -Authentication Kerberos -Credential $UserCredential #starts remote Exchange Management Shell with the credentials you entered above.
Import-PSSession $Session #imports the Exchange session from Exchange into your PowerShell so you can use its commands in your script.
Import-Module ActiveDirectory #imports the ActiveDirectory commands from your local machine, you need the RSTAT tools installed for this to work.
Set-ADServerSettings -ViewEntireForest $true #tell Exchange to scan the full AD forest, child domains, etc.
function Get-ADAccount #creates a function the code gets placed between a { and a } called a "script block"
[string]$FnameFirst = $(Read-Host "Enter the employees first and last names"))# prompts the user to enter a name and stores it as text in a variable named $fnamfirst.
$NameSplit = $FnameFirst -Split '\s+' #splits the text into two seperate objects
$LnameFirst = $NameSplit[1]+ ", " +$NameSplit[0] #reverses the order of the name (our AD is last name first) and adds a comman between. In PowerShell the first number is 0 not 1.
Get-ADUser -Filter "DisplayName -like '$($LnameFirst)*'"|select SamAccountName -ExpandProperty SamAccountName #uses AD lookup the name and then select the SamAccountName.
function Get-EmailAddress
$userlogon = Get-ADAccount #runs the Get-ADaccount function above to get the SamAccount (AD logon) and store it as a variable.
Get-Mailbox -Identity $userlogon|select emailaddresses -ExpandProperty emailaddresses #uses Exchange to find the users mailbox and select the emailaddresses field then expands it to show them all.
Get-EmailAddress #executes the the Function Get-EmailAddresses to show the results.

Line 13 is where the magic happens. We  take the First Name, Last Name and reverse it, adding a comma between them. Now our user name will match the displayname property in our backwards Active Directory tree and we can find it using a filtered Get-ADUser query.

If you need to do this with a list of usernames instead of an interactive prompt just change the $fnamefirst variable to your datasource.

$FnameFirst = Get-Content myfile.txt
$FnameFirst = Import-CSV -path c:\path\to\my\file.csv |select firstnamecolumn, lastnamecolumn -expandproperty firstnamecolumn,  lastnamecolumn -notypeinformation