Search code examples
windowspowershellcsvmultiple-columnspowershell-2.0

Powershell read in csv columns from large number of files then output to new csv where each file is a new column


I have a number of files i want to read a specific column in from each, then output each of those to one new csv file, with each file read in as a new column. I currently have the reading in and outputting to a new file working, but it outputs everything into one column, how do i separate out each files data?

This is what i have currently

Import-Csv -Delimiter "`t" -Header @("a","b","c","d","e","f","g","h","i") (Get-ChildItem -path C:\Users\xxxxx *.tsv) |
    select g |
    Export-Csv newfile.csv -NoType

Solution

  • Feed the output from Get-ChildItem as input to ForEach-Object, then import/extract/export from each file separately:

    Get-ChildItem -path C:\Users\xxxxx *.tsv |ForEach-Object {
        Import-Csv -LiteralPath $_.FullName -Delimiter "`t" -Header @("a","b","c","d","e","f","g","h","i") |
        Select-Object g |
        Export-Csv ($_.BaseName + '_newfile.csv') -NoTypeInformation
    }