0

I have been working on importing a CSV in Powershell to then export only specific columns 2,3 and 5 and ignoring the last line.

TestFile.csv:

"1","2","3","4","5"
"1","2","3","4","5"
"1","2","3","4","5"
"1","2","3","4","5"
"1","2","3","4","5"
,,,2,2,2,,,2,,

PowerShell script used:

Import-Csv '\\DESKTOP-QC1GB24\Allpay DD\Processing\TestFile.csv' -Header (1..5|%{"Column$_"}) |
    Select-Object Column2, Column3, Column5 -SkipLast 1 |
    Export-Csv -Path "\\DESKTOP-QC1GB24\Allpay DD\Completed\New.CSV"

Results I am getting (new.csv):

#TYPE Selected.System.Management.Automation.PSCustomObject
"Column2","Column3","Column5"
"2","3","5"
"2","3","5"
"2","3","5"
"2","3","5"
"2","3","5"

The problem I face is its exporting the temp column headers and some text above my data. Is there a way to exclude this? Also id like to keep the original name of the file is there a way to export with original filename + _new?

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
ryall579
  • 83
  • 1
  • 7
  • 2
    Try the `-NoTypeInformation` switch of `export-csv` to exclude the first line in your export. – Paxz Feb 25 '19 at 12:47
  • 1
    to get rid of the column headers, you will need to remove them manually. [*grin*] you can either re-read the file with `Get-Content`, or insert a step to use `ConvertTo-CSV` so that you can remove the required-to-be-valid header line before writing the file out with `Set-Content`. – Lee_Dailey Feb 25 '19 at 12:52

0 Answers0