Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Powershell to merge Excel columns

Posted on 2010-08-30
5
Medium Priority
?
1,067 Views
Last Modified: 2012-08-13
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.  

Thanks for your help.    
0
Comment
Question by:acronie18
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 13

Accepted Solution

by:
soostibi earned 2000 total points
ID: 33560638
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"}}

Open in new window

0
 

Author Comment

by:acronie18
ID: 33561294
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}}

Open in new window

0
 
LVL 13

Expert Comment

by:soostibi
ID: 33561825
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
 
LVL 13

Expert Comment

by:soostibi
ID: 33561834
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"}}

Open in new window

0
 

Author Comment

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

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question