Combine two variables from a CSV file

Hello,
I am trying to get the script below to work in bulk, from a CSV file. The issue is that the variable for the Exchange mailbox alias $ExAlias is designed to grab just the first letter of the $FirstName variable and combine it with the entire $LastName variable. This works fine when I manually populate the $FirstName and $LastName variables, but I was wondering if I can use a CSV file that has FirstName and LastName as data, but still can use my trick of just grabbing the first initial of the FirstName and adding it to full LastName to construct the $ExAlsias variable. Below is how I do it without a CSV file.

$FirstName = ‘Some’
$LastName = ‘Newemployee’
$ExAlias = $FirstName.SubString(0,1)+$LastName
$ExtMail = ‘External.Mail@othercompany.com’
$username = ‘EMP7412’
$database = ‘EX-DB-05’

Enable-Mailbox -Identity $username -Alias $ExAlias -Database $database

New-MailContact -FirstName $FirstName -LastName $LastName -Name “${FirstName} ${LastName} CORP” -Alias “${FirstName}${LastName}CORP” -OrganizationalUnit “OU=Contacts,DC=mycompany,DC=com” -ExternalEmailAddress $ExtMail

Set-Mailbox $ExAlias -DeliverToMailboxAndForward $False -ForwardingAddress $ExtMail

Generous_Items,
Welcome to the forum. :wave:t4:

BTW: When you post code or sample data or console output please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.
Thanks in advance

Why shouldn’t that work with another variable? When you use a CSV file as input you use a loop and assign the contents of the cells from the CSV file in each loop iteration to a variable you can use inside the loop. … like this:

$CSVInputData =
@'
FirstName,LastName
Jon,Doe
Jane,Doe
Albert,Einstein
Marie,Curie
Max,Planck
Nicola,Tesla
Generous,Items
'@ |
    ConvertFrom-Csv

foreach ($Line in $CSVInputData) {
    'The first letter of the firstname is: "{0}" and the lastName is "{1}"' -f $Line.FirstName.SubString(0,1), $Line.LastName
}

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.