locked
Syntax for importing names from CSV into PowerShell RRS feed

  • Question

  • I have a CSV I want to import into Powershell but I'm unsure of the syntax I need to use to accomplish the desired outcome. The CSV has a field similar to the example below

    Name
    Smith, John

    Where Smith, John is in one field instead of separate for First and Last. Basically I want to be able to take the first and last names from that one field in the CSV and place them into another part of the powershell script I'm working with so that the name shows up as JohnSmith. Any ideas if this is possible?

    Wednesday, January 28, 2015 8:49 PM

Answers

  • If you've already got your csv imported such that "Smith,John" is in one variable, you can rearrange the string the way you want by doing something like this:

    $Split = $Value.Split(",")
    $NewValue = $Split[1] + $Split[0]

    If you wanted to do it in batch, you could do:

    $ItemList | Select-Object -Property @{Name="NewName";Expression={$Split = $_.Name.Split(",")
    $Split[1] + $Split[0]}}

    Note, both of these will give you problems if you have any records where there is only one name (trying to access $Split[1] will error, because it won't exist).

    • Marked as answer by AnnaWY Monday, February 9, 2015 8:13 AM
    Wednesday, January 28, 2015 10:10 PM

All replies

  • That is NOT a CSV but this will probably work

    Import-Csv myfile.csv -del ';'


    ¯\_(ツ)_/¯


    • Edited by jrv Wednesday, January 28, 2015 9:13 PM
    Wednesday, January 28, 2015 9:13 PM
  • I know how to import a csv, I just need to take the name that is being displayed as last,first in one field and have it show up in another part of the powershell script I'm working on as firstlast. 
    Wednesday, January 28, 2015 9:24 PM
  • I know how to import a csv, I just need to take the name that is being displayed as last,first in one field and have it show up in another part of the powershell script I'm working on as firstlast. 

    What is "firstlast". Are yu saying you want to remove the comma?

    Alternate method:

    Import-Csv myfile.csv -header last,first | select first, last


    ¯\_(ツ)_/¯

    Wednesday, January 28, 2015 9:36 PM
  • Or this:

    PS C:\scripts> (import-csv fl.csv -Header last,first|select first,last)[1..99]|Export-Csv newnaems.csv
    PS C:\scripts> import-csv newnaems.csv
    
    first                                                                                    last
    -----                                                                                    ----
    John                                                                                     Smith
    Mary                                                                                     Contrary
    Smoke                                                                                    Black
    
    


    ¯\_(ツ)_/¯

    Wednesday, January 28, 2015 9:39 PM
  • Whats up man,   I think this is what you want to do.

    //SCRIPT below

    $List = Import-Csv -Path C:\temp\allusers.csv

    foreach ($row in $List)
    {

    $ee = $row.COLUMNNAME1INCSV

    $ee2 = $row.COLUMNNAME2INCSV

    }




    • Proposed as answer by sonnysucks Wednesday, January 28, 2015 9:51 PM
    • Edited by sonnysucks Wednesday, January 28, 2015 9:52 PM
    Wednesday, January 28, 2015 9:49 PM
  • If you've already got your csv imported such that "Smith,John" is in one variable, you can rearrange the string the way you want by doing something like this:

    $Split = $Value.Split(",")
    $NewValue = $Split[1] + $Split[0]

    If you wanted to do it in batch, you could do:

    $ItemList | Select-Object -Property @{Name="NewName";Expression={$Split = $_.Name.Split(",")
    $Split[1] + $Split[0]}}

    Note, both of these will give you problems if you have any records where there is only one name (trying to access $Split[1] will error, because it won't exist).

    • Marked as answer by AnnaWY Monday, February 9, 2015 8:13 AM
    Wednesday, January 28, 2015 10:10 PM
  • I see that Wednesday is still the half-fast answer day.

    ¯\_(ツ)_/¯

    Wednesday, January 28, 2015 10:23 PM
  • This is correct!  

    $List = Import-Csv -Path C:\temp\allusers.csv
    
    foreach ($row in $List)
    {
    
    $ee = $row.COLUMNNAME1INCSV
    
    $ee2 = $row.COLUMNNAME2INCSV
    
    }

    • Proposed as answer by smcmanigle Thursday, January 29, 2015 2:00 AM
    Thursday, January 29, 2015 2:00 AM
  • Guys and gals - you need tolearn how CSVs and PowerShell work,.  Just guessing at this is not helpful.

    A CSV is a well known data entity.  It has specific behaviours.  If you do not understand this then your guesses wil be worthless.

    See: http://en.wikipedia.org/wiki/Comma-separated_values

    Read - learn. Increase you knowledge.


    ¯\_(ツ)_/¯

    Thursday, January 29, 2015 2:09 AM