Powershell read CSV file line by line and only first header

I have a CSV file, which reads like this:

read, book

read1, book1

read2, book2

I want to read only the first value, before comma one line at time,

meaning first time it will be

read

read1

read2

no headers and i want to pass that value to a variable for a if condition.

I have script it read line by line but it reads both values before coma and after line by line, so instead of having three values i'm ending with 6 values.

$file = Import-CSV c:\script\server.csv

$file | ForEach-Object {
        foreach ($property in $_.PSObject.Properties) 
{        
$property.Name
$property.Value
#replace = $property.Value

}

    }


April 23rd, 2015 6:49am

Assuming that your csv has two (or more) columns, I would advice to use headers.

That way you can use the header to just call the collumn you want:

server.csv:

reading, books

read, book

read1, book1,
read2, book2

now reading and books are the header names.

$file = Import-CSV c:\script\server.csv 

$file.reading will hold what you want. (and can of course be piped)

Is there a specific reason you don't want headers?


Free Windows Admin Tool Kit Click here and download it now
April 23rd, 2015 7:47am

Hi,

Assuming csv file looks like the following one :

read,book
read1,book1
read2,book2

There are 2 ways to obtain what you expect :

  1. By importing CSV file :
$file = Import-Csv .\input.csv 
foreach ($line in $file)
{
    $line.Read | out-host;
}

This operation displays the following output :

read1
read2
  1. Without headers :
$file = Get-Content .\input.csv
for ($i = 1; $i -lt $file.Length; $i++)
{ 
    ([string]$file[$i]).Split(',')[0] | out-host;
}

produces the following output :

read1
read2

Regards,

Rgis

April 23rd, 2015 9:10am

Read first half of line one line at a time:

Get-Content c:\script\server.csv |%{$_.Split(',')[0]}

This will read all lines including first line..

To do the same with CSV you need to supply custom header

Import-Csv c:\script\server.csv -header Col1,Col2 | %{$_.Col1}

Are you sure you want to output the first line?

Free Windows Admin Tool Kit Click here and download it now
April 23rd, 2015 9:18am

I can work with headers
April 23rd, 2015 10:30am

i need to out put one line at a time, and only the first column
Free Windows Admin Tool Kit Click here and download it now
April 23rd, 2015 10:32am

If you can work with headers then do so as it will make your life easier.

with a csv file called server.csv like this:

headername1, headername2
read, book
read1, book1,
read2, book2

and this bit of code 

$file = Import-CSV c:\script\server.csv #output to host, file or directly pipe the command above. foreach($cell in $file.headername1){

if($cell -eq $something){

}

}

will evaluate the content of each cell to $something.

This is because Powershell will grab the first row and claim that as a header name.

So whatever you put in cell A1 in excell will end up as name of the first collumn and its corresponding property (e.g. $file.A1 oor $file.headername1 if you will).


  • Edited by Koen Halfwerk 16 hours 10 minutes ago extended answer
April 23rd, 2015 10:57am

You are all still missing the point and the question.  If the first line is a header then jusy use Import. If you want the first line as data then add the header in the import:

Import-Csv c:\script\server.csv -header col1,col2 | %{$_.Col1}

This will extract all of the first column values. No need for any fancy wring of many lined scripts. It is just a  normal PS operation.

Free Windows Admin Tool Kit Click here and download it now
April 23rd, 2015 11:09am

# Assuming a CSV as such:
# Name, Book
# name1, book1
# name2, book2
# name3, book3

$csv = Import-Csv C:\users\Administrator\Desktop\test.csv

# This retrieves *all* 'cells' in the column 'Name' (which happens to be the first column)
# $csv.Name

# If we want to evaluate one 'cell' at a time (line by line)
# We just pass it to a ForEach-Object
# Notice I'm still preserving .Name which means only the column 'Name'
# is up for evaluation. In this case, the column 'Name' is the first column.

$csv.Name | ForEach-Object {
    # In here, we do whatever want to the 'cell' that's currently in the pipeline
    # For now, let's just output it.
    $_

    # And to prove that we processed each 'cell' one at a time, let's also output...
    "Hello World"
}

# So if this works, we should have the follow
# Name1
# Hello World
# Name2
# Hello World
# Name3
# Hello World

Here's some code that should help explain this better.

Notice I saved the contents of the CSV all in memory, so when you're ready to read the second column (if you want to read it that is) you don't need to do Import-Csv again, you just do the same thing I did above, but for the second column $csv.Book.

April 23rd, 2015 11:35am

Assuming that your csv has two (or more) columns, I would advice to use headers.

That way you can use the header to just call the collumn you want:

server.csv:

reading, books

read, book

read1, book1,
read2, book2

now reading and books are the header names.

$file = Import-CSV c:\script\server.csv 

$file.reading will hold what you want. (and can of course be piped)

Is there a specific reason you don't want headers?


Free Windows Admin Tool Kit Click here and download it now
April 23rd, 2015 11:46am

Somebody please read the whole question. Think about it before answering.

April 23rd, 2015 12:56pm

jrv, my point was merely to demonstrate how the columns become properties of an object when imported via Import-Csv and that they can be iterated one at a time using a ForEach-Object (as an example).

You had already answered the part about the headers which does not appear to be the bit that the OP appears to be having most issues with coming to grasps with, so I didn't feel it important to repeat that.

Free Windows Admin Tool Kit Click here and download it now
April 23rd, 2015 1:01pm

Hi,

Assuming csv file looks like the following one :

read,book
read1,book1
read2,book2

There are 2 ways to obtain what you expect :

  1. By importing CSV file :
$file = Import-Csv .\input.csv 
foreach ($line in $file)
{
    $line.Read | out-host;
}

This operation displays the following output :

read1
read2
  1. Without headers :
$file = Get-Content .\input.csv
for ($i = 1; $i -lt $file.Length; $i++)
{ 
    ([string]$file[$i]).Split(',')[0] | out-host;
}

produces the following output :

read1
read2

Regards,

Rgis

  • Proposed as answer by Régis Lainé Thursday, April 23, 2015 1:09 PM
April 23rd, 2015 1:09pm

If you can work with headers then do so as it will make your life easier.

with a csv file called server.csv like this:

headername1, headername2
read, book
read1, book1,
read2, book2

and this bit of code 

$file = Import-CSV c:\script\server.csv #output to host, file or directly pipe the command above. foreach($cell in $file.headername1){

if($cell -eq $something){

}

}

will evaluate the content of each cell to $something.

This is because Powershell will grab the first row and claim that as a header name.

So whatever you put in cell A1 in excell will end up as name of the first collumn and its corresponding property (e.g. $file.A1 oor $file.headername1 if you will).


  • Edited by Koen Halfwerk Thursday, April 23, 2015 2:59 PM extended answer
Free Windows Admin Tool Kit Click here and download it now
April 23rd, 2015 2:56pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics