cmoerbe
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')
}
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
From now on I am just going with import / where-object /export.
Thanks for the help!