2

I have a problem with my code, when I export in the file only one object is displayed in the file, an example should be better:

There is the result :

Field1
Test1

And I want to get this :

Field1  col1    col2   Field2   col3   col4
Test1   a       b      Test2     c      d
        a2      b2               c2     d2

The file :

col1    col2
a       b
a2      b2

I've tried my best to produce a piece of code, hope this will help. I create the field in the code.

$csv = Import-Csv .\file.csv -Delimiter ';'
$dataTable = New-Object System.Data.DataTable

$dataTable.Columns.Add("Field1") | Out-Null


foreach ($s in "Test1")
{
  $row = $dataTable.NewRow()

  $row["Field1"] = $s

  $dataTable.Rows.Add($row)
}

#my problem is there, I don't know how merge these two lines
$dataTable | export-csv -NoTypeInformation -Path ".\fileResult.csv"

$csv | Export-CSV   -Path ".\fileResult.csv" -NoTypeInformation -Delimiter ";" -Append
2
  • You're only adding one column to the table... Do you not need to add the others too? Commented Jun 28, 2017 at 10:00
  • Sry, I didn't show all the expected file but my problem is in the title. I have two lines and I don't know how to merge that to get the result at the top. Or maybe it's not called object ? Commented Jun 28, 2017 at 10:01

1 Answer 1

2

In you example, you have headings and one row. I assume you would line to loop over multiple rows.

Running all of this code will give you errors; either use the single-line or looping version.

$csv = Import-Csv .\file.csv -Delimiter ';'

#Single line
$csv | Add-Member -Type Noteproperty -Name "Field1" -Value "Test1"
$csv | Add-Member -Type Noteproperty -Name "Field2" -Value "Test2"

#looping over all lines
foreach($line in $csv({
    $line | Add-Member -Type Noteproperty -Name "Field1" -Value "Test1"
    $line | Add-Member -Type Noteproperty -Name "Field2" -Value "Test2"
}

# use Select-Object to get orders CSV
$csv | Select-Object Field1, col1, col2, Field2 | Export-CSV   -Path ".\fileResult.csv" -NoTypeInformation -Delimiter ";" -Append

Edit: some explanation

Your CSV file is imported as an object. The headings (col1, col2) are automatically assigned as NoteProperty members of the object. You can see this by running $csv | Get-Member.

You can add members to the CSV object using Add-Member, specifying the member type, Name (heading) and value.

Documentation

Import-Csv
Add-Member
Export-Csv
Select-Object

Sign up to request clarification or add additional context in comments.

7 Comments

There is an error in your code no ? There is only one "(".
When I run your code I have the two fields after the columns is it possible to have the Field1 at the first cell ?
@Teslo Yes, if you run it all. Please see text; either use the single line version (which will give you the results in the question) or the multiline (which is properly what you want, if you have more than one row in your csv). Using both will generate errors
@Teslo See edit - Select-Object will allow you to order
I found a problem too, The -Append without this, the code works, I working on it and I check the possibilities. Thanks for your help. Can I ask you a question if I don't understand something i the this context ?
|

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.