1

I'm new to Powershell but I've given it my best go.Have a .csv file, small example:

id,location_id,name,title,email,directorate 
1,1, Amy lee,Singer,, 
2,2,brad Pitt,Actor,,Production 
3,5,Steven Spielberg,Producer,[email protected],Production

Need to:

  • change first and last name to uppercase, example, Brad Pitt, Amy Lee.
  • create email with pattern first letter of first name + last name, all in lowercase with @google.com and value from location_id, example - [email protected], [email protected]
  • save it to new file.csv, with the same structure, example:
id,location_id,name,title,email,directorate 
1,1, Amy Lee,Singer,[email protected], 
2,2,Brad Pitt,Actor,[email protected],Production 
3,5,Steven Spielberg,Producer,[email protected],Production

I wrote a script, whose commands one by one perform the task, but dunno how to save it no new .csv file:

param (
    [string] $file_path
)

$inputFile = Import-Csv -Path $file_path

foreach ($line in $inputFile) {
    $line.name = (Get-Culture).TextInfo.ToTitleCase($line.name)
    $firstName = $line.name.split(" ")[0]
    $lastName = $line.name.split(" ")[1]
    $newEmail = ($firstName[0] + $lastName + $line.location_id + "@google.com").toLower()
} 

Perhaps, there is more clean solution?

1 Answer 1

1

Your code is close to completion but here is a simpler way. You're already using TextInfo.ToTitleCase but for the email address you could simplify it using the -replace operator and a bit of regex.

You can find info about the regex pattern in this link: https://regex101.com/r/WKF9R5/1

$txtInfo = [cultureinfo]::InvariantCulture.TextInfo
$csv = Import-Csv path\to\csv.csv

foreach($line in $csv) {
    $name = $line.name

    $line.name  = $txtInfo.ToTitleCase($name)
    # Example: 'brad Pitt' => bPitt
    $line.email = ($name -replace '(?<=^\w{1})\w+\s').ToLower() + $line.location_id + '@google.com'
}

$csv | Export-Csv path\to\newcsv.csv -NoTypeInformation

Final output using the Csv in the question:

id location_id name             title    email                  directorate
-- ----------- ----             -----    -----                  -----------
1  1           Amy Lee          Singer   [email protected]
2  2           Brad Pitt        Actor    [email protected]      Production
3  5           Steven Spielberg Producer [email protected] Production
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very much! Can I ask you, to explain what’s going on in a first line of script?
@lunnyj the cultureinfo one? It's just the same as Get-Culture
can you help me again?:) how I can save new file in the same directory where old file was located? Run my script, ./my_script.ps1 file.csv which located at the same directory where file.csv stored. But when I run script from the another directory, new file save in directory when the script located, but I need to save it on the same directory, where file.csv stored.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.