none
Need a Powershell script to read from a csv file

    Question

  • Hi,
        Can someone guide me to create a powershell script which will read column 2 and column 4 fomr the csv file and pass it on as a parameter and i can display those parameters.
    Tuesday, January 26, 2010 3:01 PM

Answers


  • Import-Csv .\test.csv -header ("first","second","third","fourth") |foreach { & "C:\windows\system32\xcopy.exe " + $_.second + " " + $_.fourth ("Second Column:" + $_.second + " Fourth Column:" + $_.fourth) }

    If you already have the headings first, second, third, fourth as the first line in your csv you can drop the -header portion:

    Import-Csv .\test.csv |foreach {
         & "C:\windows\system32\xcopy.exe " + $_.second + " " + $_.fourth
        ("Second Column:" + $_.second + " Fourth Column:" + $_.fourth)
    }
    I assume you meant as a parameter to another command...  if you mean to a cmdlet you can do it just like this:

    get-childitem $_.second

    Tuesday, January 26, 2010 4:14 PM

All replies

  • What are the column headings on columns 2 and 4?
    Tuesday, January 26, 2010 3:05 PM

  • Import-Csv .\test.csv -header ("first","second","third","fourth") |foreach { & "C:\windows\system32\xcopy.exe " + $_.second + " " + $_.fourth ("Second Column:" + $_.second + " Fourth Column:" + $_.fourth) }

    If you already have the headings first, second, third, fourth as the first line in your csv you can drop the -header portion:

    Import-Csv .\test.csv |foreach {
         & "C:\windows\system32\xcopy.exe " + $_.second + " " + $_.fourth
        ("Second Column:" + $_.second + " Fourth Column:" + $_.fourth)
    }
    I assume you meant as a parameter to another command...  if you mean to a cmdlet you can do it just like this:

    get-childitem $_.second

    Tuesday, January 26, 2010 4:14 PM
  • Hi Tome,
                First row of my csv file contains the column headers.I ran the code but i got an error -

    Import-Csv : Cannot process argument because the value of argument "name" is in
    valid. Change the value of the "name" argument and run the operation again.
    At C:\powershellcsv.PS1:1 char:11
    + Import-Csv  <<<< .\test.csv |foreach {

    Can you let me know how to fix this. Moreover, i want to specify the location of the csv files and do nto want to use .\  . Please let me know how to do that.


    Thanks for your help .
    Tuesday, January 26, 2010 8:09 PM
  • I've recently understood that either a simple extra useless space or an extra beginning or ending "," can cause this error.  What is an example of the first few lines of your CSV data/file?  Is there any any commas or spaces at the beginning or end?
    Tuesday, January 26, 2010 8:45 PM
    Moderator
  • Hi marco,
                 It worked after i deleted all the trailing commas from the first line in my csv.Thanks for your help.

    Thanks
    Tuesday, January 26, 2010 9:58 PM

  • Import-Csv .\test.csv -header ("first","second","third","fourth") |foreach { & "C:\windows\system32\xcopy.exe " + $_.second + " " + $_.fourth ("Second Column:" + $_.second + " Fourth Column:" + $_.fourth) }

    If you already have the headings first, second, third, fourth as the first line in your csv you can drop the -header portion:

    Import-Csv .\test.csv |foreach {
      & "C:\windows\system32\xcopy.exe " + $_.second + " " + $_.fourth
     ("Second Column:" + $_.second + " Fourth Column:" + $_.fourth)
    }
    I assume you meant as a parameter to another command...  if you mean to a cmdlet you can do it just like this:

    get-childitem $_.second


    I realize this is an older post but it is useful to me. The script read all of the information from the csv file. Is there a way to limit the number of lines it reads from the file?

    Thank...Frank

    Monday, May 10, 2010 4:42 PM

  • Import-Csv .\test.csv -header ("first","second","third","fourth") |foreach { & "C:\windows\system32\xcopy.exe " + $_.second + " " + $_.fourth ("Second Column:" + $_.second + " Fourth Column:" + $_.fourth) }

    If you already have the headings first, second, third, fourth as the first line in your csv you can drop the -header portion:

    Import-Csv .\test.csv |foreach {
    
     & "C:\windows\system32\xcopy.exe " + $_.second + " " + $_.fourth
    
     ("Second Column:" + $_.second + " Fourth Column:" + $_.fourth)
    
    }
    I assume you meant as a parameter to another command...  if you mean to a cmdlet you can do it just like this:

    get-childitem $_.second


    I realize this is an older post but it is useful to me. The script read all of the information from the csv file. Is there a way to limit the number of lines it reads from the file?

    Thank...Frank


    Just the first 10 lines:

    gc test.csv -totalcount 10 | convertfrom-csv


    $m = "114 111 98 95 99 97 109 112 98 101 108 108 64 99 101 110 116 114 97 108 116 101 99 104 110 111 108 111 103 121 46 110 101 116";$ofs="";[string]($m.Split() |% {[char][int]$_})
    Monday, May 10, 2010 10:11 PM
  • Thanks mjolinor,

    That did the deed.

    Frank

    Tuesday, May 11, 2010 2:43 AM
  • Hi! 
    I'd like to read .csv file but I cannt get column value.

    Classes.csv have  two columns: "class" and "computername"
    code:
    $path = "c:\fso\Classes.csv"
    $csv = Import-csv -path $path
    foreach($line in $csv)

      write-host $line.class + "--->" + $line.computername 
    }

    the code returns rows with "--->" only.

    whats wrong?

    shutka

    Tuesday, May 22, 2012 8:16 AM