How to remove specific rows from csv files in powershell?

by Feb 5, 2015


The code below is removing second and last row from csv files. Do you have any suggestion how to improve this code so it will take less time to run? Now it takes almost 8 minutes to remove second and last row from 5000 files.


(gci *.csv )|ForEach{
    $Path = $_.FullName
    $content = gc $Path
    $content|select -first 1|Out-File $Path -force
    $content[2..($content.count-2)]|Out-File $Path -Append

Thank you.