MSH: Various Filters

Filter functions are similar to functions except that a filter function is meant only to be used in a pipe.  The filter is called once for each object in the pipe and you refer to the current pipeline object via $_. 

# ———————————————————————
# Handy filter functions
# ———————————————————————
filter sa {
    if ($_.Name -notmatch "([gs]et_*)|(op_*)") {
        write-object $_
    }
}

filter pick([string] $prop) {
    $_.$prop
}

filter typename {
    $_.GetType().Name
}

The first filter works around an annoying behavior of the get-member cmdlet since it shows the get/set accessor methods in addition to the the properties for an object.  Just run the result through "sa" to strip out those accessor methods e.g.:

> get-process | get-member | sa

Note that this filter leaves in the add/remove event hookup methods since this is the only way at present in MSH to connect to an event. 

The second filter allows you to pick off a property from the object piped in.  The select-object cmdlet performs a similar function but it also wraps the property in a MshCustomObject which isn’t always what you want.  You use the second filter like so:

> get-command dir | pick Definition

The last filter is one I use as I spelunk around MSH trying to figure out what objects are being passed down the pipe e.g.:

> dir c:\ | typename

Advertisements
This entry was posted in Monad. Bookmark the permalink.

One Response to MSH: Various Filters

  1. Unknown says:

    Take care as not all Get_ / Set_ methods make it into a property,so as this is usualy handy, also check in the "normal way" sometimes.gr /\\/\\o\\/\\/

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s