LINQ like functions for PowerShell: Skip-Count


The PowerShell pipeline, is fairly similar to C#/VB’s LINQ.  Both filter a group of elements through a series of transformations which produce a new series of elements.  The devil is in the details of course but I’ll get to that in a future post. 

When using PowerShell I constantly find myself wanting to use various LINQ expressions on a pipeline.  Unfortunately, many LINQ expressions have no built-in equivalent in PowerShell.  Most are fairly straightforward to write but a few are a bit trickier.  In either case, there’s no reason for people needing to figure them out twice.  So I’ll be starting a series on LINQ expressions in PowerShell.

Also, my posts are getting a bit long winded as of late.  This will be a good oppuritunity to get some shorter posts up.

Today’s entry is the equivalent of Enumerable.Skip.  The operation takes a count and skips “count” elements in the enumeration.  For PowerShell, it’s the equivalent of skipping “count” elements in the pipeline. 

#============================================================================
# Skip the specified number of items
#============================================================================
function Skip-Count() {
    param ( $count = $(throw "Need a count") )
    begin { 
        $i = 0
    }
    process {
        if ( $i -ge $count ) { 
            $_
        }
        $i += 1
    }
    end {}
}

Example:

PS:) 1..10 | skip-count 5
6
7
8
9
10
Comments (2)

  1. Great minds think alike! I actually implemented an assload of these kinds of commands in C# using a Cmdlet. I work with a lot of large sequences and the performance of a compiled C# Cmdlet blows away script-based ones but for my blog I’m gonna convert them to advanced functions.

    Here’s one that I came up with that you might like:

    http://einsteintech.spaces.live.com/blog/cns!89E05724AF67A39E!228.entry

  2. jaredpar says:

    @Josh,

    I love the default "lambda" expression inline in the parameter list.