Rob_C Rob_C - 1 year ago 181
PowerShell Question

copy and alter column in CSV

I will daily be needing to modify a CSV so I can provision accounts. It requires simple information in and creating new accounts. I can handle the PowerShell for creating accounts in AD, it's modifying the CSV. I need to create a username and an email address with additional text.

Input will look like this:

Joe,Johnson,High School,A2F,teacher
Greg,Harris,Middle School,A2F,teacher

Output needs to look like this:

Joe,Johnson,High School,A2F,teacher,JJohnson.sfo,
Greg,Harris,Middle School,A2F,teacher,GHarris.sfo,

Answer Source

Use Select-Object with a calculated property for each new column you want:

Import-Csv .\old.csv |
Select-Object *,@{Name='Username';Expression={'{0}{1}.sfo' -f $_.First[0],$_.Last}},@{Name='Email';Expression={'{0}.{1}' -f $_.First,$_.Last}} |
Export-Csv .\new.csv -NoTypeInformation
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download