[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 1070

# Powershell to merge Excel columns

I would like to use Powershell to create a username and email address from a csv file that contains 2 columns, first name and last name.  The username is derived from the first initial of the first name plus last name.

0
acronie18
• 3
• 2
1 Solution

Commented:
As a start look at this.
What kind of output would you like to have?
There might be problems, if you use such a character set, that include characters that are not accepted as username or email address. Also, your algorithm can result in multiple users with the same username and email address. If such occurs then tell me, I'll further improve the script.
Import-Csv c:\ee\names.txt |
Select-Object @{n="username"; e={$_.firstname[0]+$_.lastname}}, @{n="email";e={"$($_.firstname[0]+$_.lastname)@yourdomain.com"}}  0 Author Commented: That works well. I need all to output all four columns. I added the below code and was able to get what I was looking for. Just so I can wrap my brain around it, are 'n' and 'e' just variables or do they mean something specific? Thanks for your help. @{n="firstname"; e={$_.firstname}}, @{n="lastname"; e={$_.lastname}}  0 Commented: If you need all four columns, you can simply use: Import-Csv c:\ee\names.txt | Select-Object firstname, lastname, @{n="username"; e={$_.firstname[0]+$_.lastname}}, @{n="email";e={"$($_.firstname[0]+$_.lastname)@yourdomain

So you do not have to use the complicated hashtable syntax.
0

Commented:
Some characters are missing:
Import-Csv c:\ee\names.txt |
Select-Object firstname, lastname, @{n="username"; e={$_.firstname[0]+$_.lastname}}, @{n="email";e={"$($_.firstname[0]+\$_.lastname)@yourdomain.com"}}

0

Author Commented:
That makes much more sense.  I was trying to do something similar, but I over complicated it.  Thanks for your help.
0

## Featured Post

• 3
• 2
Tackle projects and never again get stuck behind a technical roadblock.