kardon
kardon

Reputation: 35

How to select column from CSV table

I need a script that generates usernames based on the first, last name and adds a couple of random numbers. How do I tell PowerShell to separate the columns and not write them all in one?

This is my current code. I was thinking maybe I can separate the names on the semicolon. And then select a part of them with substring and generate my number, put them together and voila.

$users = Import-Csv -Path C:\Users\username\Desktop\Namen.csv -Header "Vorname","Nachname","Username"

foreach ($user in $users) {
    $user -replace ';', ' '
}

Upvotes: 0

Views: 91

Answers (1)

Razorfen
Razorfen

Reputation: 433

It seems that the only thing missing is the -delimiter ";" in your Import-Csv.

You can access/manipulate your data like so:

$csv_file = ".\Namen.csv"
$users = Import-Csv $csv_file -Delimiter ";" -Encoding "default"

foreach ($user in $users) {
    $generated_username = $user.Vorname + $user.Nachname + "1234"
    Write-Host "Vorname:           " $user.Vorname
    Write-Host "Nachname:          " $user.Nachname
    Write-Host "Username:          " $user.Username
    Write-Host "Generated username: $generated_username"
    Write-Host ""
}

Upvotes: 1

Related Questions