Powershell select-object skip multiple lines?

If I need to select only some lines, I would directly index into the array:

$x = gc c:\test.txt
$x[(0..2) + (8..($x.Length-1))]

It is also possible to create a function Skip-Objects

function Skip-Object {
    param(
        [Parameter(Mandatory=$true,ValueFromPipeline=$true)][PsObject]$InputObject,
        [Parameter(Mandatory=$true)][int]$From,
        [Parameter(Mandatory=$true)][int]$To
    )

    begin {
        $i = -1
    }
    process {
        $i++
        if ($i -lt $from -or $i -gt $to)  {
            $InputObject
        }
    }
}

1..6 | skip-object -from 1 -to 2   #returns 1,4,5,6
'a','b','c','d','e' | skip-object -from 1 -to 2 #returns a, d, e

You can also use the readcount property to exclude lines:

get-content d:\testfile.txt | where {$_.readcount -lt 3 -or $_.readcount -gt 7}

Tags:

Powershell