How to remove First and Last Line in Powershell

Question:

This script will do some processing on csv file notably removing first line (subsequent to How to export to “non-standard” CSV with Powershell ) :

Now I need to refine it by also removing last line. I tried to add :

select -skip ($_.Count – 1)

but it generates exception.

So what the right syntax ?

Answer:

Depending on the version of PowerShell you have, you can use the -SkipLast parameter, e.g.:

SkipLast is available for PowerShell 5.0 and higher.

If you don’t have that parameter, you can install it via the Microsoft Website. Windows 7 is the earliest OS version that can run PowerShell 5.

If that’s not possible, use:

Source:

How to remove First and Last Line in Powershell by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply