Forum Discussion
kjetilj
Jul 16, 2021Copper Contributor
Importing CSV, splitting names and joining
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...
- Jul 16, 2021
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
yuzoyox
Iron Contributor
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
}
kjetilj
Jul 16, 2021Copper Contributor
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.
- yuzoyoxJul 16, 2021Iron ContributorSorry 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
}