SOLVED

Importing CSV, splitting names and joining

Copper Contributor

Hi,

Very new to PowerShell and scripting so I'm trying to do a lab exercise.

 

I'm trying to take input from a CSV file where the name is formatted as "Surname, Name". When I split this it returns as expected each surname and name on separate lines. Trying to "join" this to be "Name Surname" proves to be a challenge. I only get the first "name surname" repeated over and over again.

 

CSV

Name
Rhodes, Dorthy
Woodward, Chauncey
Horn, Julian  

 Script

$Users = Import-Csv -Path "C:\Users\user\Scripts\Labs\StuffForLabs\UserListB.csv" -Delimiter ";" |
ForEach-Object {
($_.Name -split ', ')
}
ForEach ($user in $users) {
$name = @($Users[1] + " " + $Users[0])
$name
}

Running the first part $users returns

Rhodes
Dorthy
Woodward
Chauncey
Horn
Julian

The second part $name returns the same name multiple times

Dorthy Rhodes

 

Would be very thankful for any help and pointers to what I am doing wrong. Have been banging my head against the wall for very long time now.

 

Kjetil

12 Replies

@kjetilj 

 

Hi, you need to set a variable and auto implement, because it is printing the same position

$name = @($Users[1] + " " + $Users[0])

a logic like these

$name = @($Users[i] + " " + $Users[j])

 i=1 and j=0, using i=i+2 and j=j+2. You can use a for to print the next position

Thank you!
I understand what your saying, but need to figure out how to do it 🙂

@kjetilj Hi, like these. Dont forget to flag as a solution if it works. Thank you very much

$Users = Import-Csv -Path "C:\Users\user\Scripts\Labs\StuffForLabs\UserListB.csv" -Delimiter ";" |
ForEach-Object {
($_.Name -split ', ')
}
$i=1
$j=0
ForEach ($user in $users) {
$name = @($Users[i] + " " + $Users[j])
$i=$i+2
$j=$j+2
$name
}

 

@yuzoyox Hi again 🙂
Thank you, I'm very grateful for your help. You confirmed the way I thought it was supposed to be written, but it sadly gave an ParseError.

 

 

ParserError: 
Line |
2 | $name = @($Users[i] + " " + $Users[j])
| ~
| Array index expression is missing or not valid.

 

best response confirmed by kjetilj (Copper Contributor)
Solution

@kjetilj 

Here is a possible solutions

 

$Users = Import-Csv -Path "C:\Users\user\Scripts\Labs\StuffForLabs\UserListB.csv" -Delimiter ";" |
ForEach-Object {
($_.Name -split ', ')
}
for ($i=0; $i -le ($users.count-1); $i=$i+2 ){
$Users[$i+1] + " " + $Users[$i]
}

 

 

this is the output

 

Dorthy  Rhodes
Chauncey Woodward
Julian  Horn

 

@gastone 

Great! Yes, that worked. No I need to understand it... better reread the about_for file again and compare it to your solution :smile:

Sorry I forgot to add $

$Users = Import-Csv -Path "C:\Users\user\Scripts\Labs\StuffForLabs\UserListB.csv" -Delimiter ";" |
ForEach-Object {
($_.Name -split ', ')
}
$i=1
$j=0
ForEach ($user in $users) {
$name = @($Users[$i] + " " + $Users[$j])
$i=$i+2
$j=$j+2
$name
}

@yuzoyox 

Uh, I should have seen that myself so that's on me! Just tired after steering at the same lines and comparing it to "google" for hours.

Grateful for you help, and this makes more sense to n00b !

You are on the right place, here is to solve problem and learn a new way to use powershell, like @gastone use. powershell community are very friendly

Another way to approach it:

$Users = Import-Csv -Path "C:\Users\user\Scripts\Labs\StuffForLabs\UserListB.csv" -Delimiter ";"
foreach ($user in $Users)
{
    $name = "{1} {0}" -f ($user -split ', ')
    $name
} 

Might be a little easier to understand.

Though I have complicated it with the way I have used the -f format string.

 

@psophos 

Nice, thank you! Came across similar examples when I searched for a solutions but at that time it didn't make sense to me. Now it's more clear 🙂

 

Will play around with all approaches as the CSV grows with more real life information. 

@psophosNice the idea to use -f but your code is wrong, not really, the worng part is  that @kjetilj 

using

Import-Csv -Path "C:\Users\user\Scripts\Labs\StuffForLabs\UserListB.csv" -Delimiter ";"

for reading a non csv file and the  non present delimiter ";" is necessary to correct the starting point error.

This is not a CSV!

Name
Rhodes, Dorthy
Woodward, Chauncey
Horn, Julian  

 

@psophosThis the correct code

cls
"The corrected script, for the non CSV..."
$Users = Import-Csv -Path "C:\Users\user\Scripts\Labs\StuffForLabs\UserListB.csv" -Delimiter ";"
foreach ($user in $Users)
{
    $name = "{1} {0}" -f ($user.name -split ',')
    $name
} 

 

A more readable code with some comment

# 
# skip the first line, so we have a regular csv
$reallyAcsv=Get-Content -Path "C:\Users\user\Scripts\Labs\StuffForLabs\UserListB.csv" | Select-Object -Skip 1
# Add header for readability 
$USRs=$reallyAcsv | ConvertFrom-Csv -Header 'surname','name'
# Now is easy to understand...
$USRs|foreach-object {"$($_.name)  $($_.surname)"}
# if you want only name...
$USRs|foreach-object {$_.name)}
# if you want only surname...
$USRs|foreach-object {$_.surname)}

The same code in a single line

"+ In a single line +"
Get-Content -Path "C:\Users\user\Scripts\Labs\StuffForLabs\UserListB.csv" | 
    Select-Object -Skip 1 | 
        ConvertFrom-Csv -Header 'surname','name' | 
            foreach-object {"$($_.name)  $($_.surname)"}

I hope now is more clear the different solutions
Bye Gas

1 best response

Accepted Solutions
best response confirmed by kjetilj (Copper Contributor)
Solution

@kjetilj 

Here is a possible solutions

 

$Users = Import-Csv -Path "C:\Users\user\Scripts\Labs\StuffForLabs\UserListB.csv" -Delimiter ";" |
ForEach-Object {
($_.Name -split ', ')
}
for ($i=0; $i -le ($users.count-1); $i=$i+2 ){
$Users[$i+1] + " " + $Users[$i]
}

 

 

this is the output

 

Dorthy  Rhodes
Chauncey Woodward
Julian  Horn

 

View solution in original post