Moek
Moek

Reputation: 119

"Select -skip" doesn't skip the very first line

I need to skip the first 4 lines of a csv, because line 5 includes the headers:

1 Assetliste;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2 Firma: Ospelt  Gruppe;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3 Abrufdatum: 19.02.2018 23:06;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4 User:;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
5 Header1; Header2 and so on.....

I use following PowerShell Code to Import and Export the file in the Format i need

$ImportedCSV = Import-CSV "$($importPath)\$($name.name)" -Delimiter ';' | Select -skip 4 
$ImportedCSV | ConvertTo-Csv -NoTypeInformation | % {$_ -replace '"', ""} | Out-File ("$($importPath)\$($name.name)")

But it doesn't skip the very first line which results in following Output:

1 Assetliste
2 Develop
3 Develop
4 Kyocera
5 Kyocera

If I delete the 4 lines manually and not using "select skip", my script works as planned:

1 Marke,Modell,ArtikelNr, ...
2 Develop,ineo+ 308,A7PY121, ...
3 Develop,ineo 284e,A61G121, ...
4 Kyocera,FS-4200DN,FS-4200DN, ...
5 and so on.....

Upvotes: 0

Views: 834

Answers (2)

Fourat
Fourat

Reputation: 2447

Import-Csv expects the first line to be the header. Use Get-Content instead.

$ImportedCSV = Get-Content "$($importPath)\$($name.name)" -Delimiter ';' | Select -skip 4

Upvotes: 0

Maximilian Burszley
Maximilian Burszley

Reputation: 19654

Given your case (which is a little odd), you can do the following:

$csv = Get-Content -Path "$importpath\$($name.name)" | Select-Object -Skip 4
$csv = $csv | ConvertTo-Csv -Delimiter ';' -NoTypeInformation
$csv | Export-Csv -Path "$importpath\$($name.name)" -NoTypeInformation -Force

You have to remove the offending lines before the conversion takes place.

Upvotes: 1

Related Questions