0

Just want to ask how to Export the data in existing CSV file in new column. I have this following code.

$Ex=Compare-Object $ImportWin7 $Importafipd1 -includeequal 

$Ex | Select-Object SideIndicator | Export-Csv -Append -Force  -NoTypeInformation "C:\NotBackedUp\EndpointAudit\Win7machinetest2.csv"

but it appears that the Data from $EX was appended not on the first row of the column.

2
  • Consider adding some sample data that demonstrates the problem. Commented Dec 14, 2016 at 6:03
  • You can't just add an additional column to existing csv document. You would want recreate it again with required number of columns. Commented Dec 14, 2016 at 6:12

1 Answer 1

3

You can't just add an additional column to existing csv document. You would want to recreate it again with number of columns you need. Like so:

$ExistingCSV = Import-Csv "C:\NotBackedUp\EndpointAudit\Win7machinetest2.csv"

$Ex=Compare-Object $ImportWin7 $Importafipd1 -includeequal | Select-Object SideIndicator

$obj = @()
$i = 0

foreach ($row in $ExistingCSV)
{
    $item = New-Object PSObject -ArgumentList $row
    $item | Add-Member -MemberType NoteProperty extra_column -Value $Ex[$i]
    $obj += $item
    $i++
}
$obj | Export-Csv -Append -Force  -NoTypeInformation "C:\NotBackedUp\EndpointAudit\Win7machinetest2.csv"
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.