0

I am trying to import a csv file, format the result as a table & finally run a foreach loop on each row in the table. The Import and format table piece works fine but I cannot get the foreach working.

The below works as expected:

$csvFile="C:\cvsFile.csv"
$table = Import-Csv $csvFile | Format-Table

But when I try to do a simple foreach on the $table variable the result set is blank (It doesn't throw any errors).

$csvFile="C:\cvsFile.csv" $table = Import-Csv $csvFile | Format-Table
foreach($record in $table) {    Write-Host $record.Field1 }

Is it possible to work with the output of Format-Table this way?

1 Answer 1

2

use the format-* cmdlets to format the output (display) of your results, and they should be the last cmdlet used in a pipe ! What you want may be something like this :

$c=Import-Csv c:\csv.csv
$c | foreach {
    $_ | format-table #all columns
    $_.Field1 #one column
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.