Link to home
Start Free TrialLog in
Avatar of cmoerbe
cmoerbeFlag for United States of America

asked on

Powershell - Get data from one csv file and add to another csv file where a value is true

Hello,

I have 2 csv files, an input.csv and an output.csv.

If my "Custom_Column" from the input.csv has an "X" in it, then copy the contents of that Column_A from that row in  input.csv to column_A of the output file, appending.

I went to MSDN and tried to use the suggested add-content method, but its not counting my foreach loop it seems. Instead its just appending everything from the input file to the output file.


So, if...

Input.csv

Column_A | Column B | Custom_Column
Item1                                     X

Then....

Output.csv
Column_A
Item1

Any help is greatly appreciated.

$Output = Import-CSV 'C:\Outputcsv'
$items = Import-CSV 'C:\Input.csv'

ForEach ($item in $items | Where {$_.Custom_Column  -eq 'X'})
{
Add-Content -path C:\Output.csv -value (Get-content 'C:\Input.csv)
# I also tried Add-Content -path C:\Output.csv -value (Get-content 'C:\Input.csv |Select 'Column_A')
}
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cmoerbe

ASKER

That was it.

From now on I am just going with import / where-object /export.

Thanks for the help!