Home > OS >  PowerShell: Export Data from Specific Row Number from multiple files and export to one csv
PowerShell: Export Data from Specific Row Number from multiple files and export to one csv

Time:09-23

Beginner user...

I have 30 .dat files in one folder and need to export row 10 from each file and compile into one csv file.

I know I am on the right lines but am not sure of the middle section - this is where I'm at...

    Get-ChildItem -Path C:\Users\pitters\Folder\* -Include *.dat -Recurse
    ForEach-Object { 
    Select-Object -Skip 9 -First 1 } 
    Export-CSV -Path Users\pitters\Folder\output.csv

Am I missing Get-Content and can anyone help with what needs correcting?

Thanks in advance. Matt

CodePudding user response:

As you mentioned yourself, you need to invoke Get-Content to actually read the file contents. In addition, you also need to construct an object with appropriate properties corresponding to the coumns you want in your CSV file - something we can do with Select-Object directly:

Get-ChildItem -Path C:\Users\pitters\Folder\* -Include *.dat -Recurse |ForEach-Object { 
    $file = $_
    $file |Get-Content |Select-Object @{Name='Line10';Expression={$_}},@{Name='File';Expression={$file.FullName}} -Skip 9 -First 1 
} |Export-CSV -Path Users\pitters\Folder\output.csv
  • Related