Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell CSV manipulation

Tags:

powershell

csv

I have a .csv file that contains usernames in the first column. They are in the form of FirstName LastName. I want to take the FirstName and add the first character of the LastName onto it, and delete the space. I then want to add @someemailaddress.com.

Here is the example:

This is what I have:

DisplayName, OtherColumn
Sam Jones, otherdata
Paul Jones, otherdata

This is what I want:

DisplayName, OtherColumn
[email protected], otherdata
[email protected], otherdata

Ideas?

like image 668
SMPLGRP Avatar asked Sep 06 '26 07:09

SMPLGRP


1 Answers

By using Import-Csv, Select-Object, and Export-Csv, you can set up a pipeline that gets the contents of the CSV file, selects a new calculated DisplayName property with an expression that performs your string manipulations, and exports the data back out as CSV.

Import-Csv data.csv | Select-Object @{
    Name = "DisplayName"
    Expression = {
        $parts = $_.DisplayName.Split();
        $parts[0] + $parts[1][0] +"@someemailaddress.com"
    }
}, OtherColumn | Export-Csv data-new.csv
like image 155
Adam Maras Avatar answered Sep 10 '26 09:09

Adam Maras