How does Match-String with with Pipelines of objects?

In our newsgroup (Microsoft.Public.Windows.Server.Scripting) , Vasu asked about how match-string works in pipelines:

Here is what I observe:
1. MSH C:\> get-alias
Alias           ri                                   remove-item
Alias           rni                                  rename-item

2. MSH C:\> get-alias | match-string ri
MSH C:\>

The question is why did the string "ri" get emitted instead of the alias record.

So what is going on here?

Match-String works on Streams of STRINGS.  When you pass it a string of OBJECTS, the MONAD engine converts those objects to strings before passing them to Match-String.  Thus the following 2 commands are functionaly equivalent:

get-alias | match-string "ri"
get-alias | foreach {"$_"} |match-string "ri"

Converting an object to a string is accomplished by call that object's ToString() method (vs using MONAD's formating subsystem).  Notice the difference - the first uses Monad's formating and the second is the object's ToString() 

MSH> (gal ri)

CommandType Name Definition
----------- ---- ----------
Alias ri remove-item

MSH> (gal ri).ToString()

If you wanted Match-String to work on the Monad formatted output, you'll need to get that as a string.  Here is the thing to grok about our outputing.  When your command sequence emits a stream of strings, we emit it without processing.  If  instead, your command sequence emits a stream of objects, then we redirect those objects to the command Out-Default.  Out-Default looks at the type of the object and the registered formating metadata to see if there is a default view for that object type.  A view defines a FORMATTER and the metadata for that command.  Most objects get vectored to either Format-Table or Format-List (though they could go to Format-Wide or Format-Custom).  THESE FORMATTERS DO NOT EMIT STRINGS!  You can see this for yourself by the following:

MSH> gps |ft |group {$_.GetType().name} |ft Count,Name -auto

Count Name
----- ----
1 FormatStartData
1 GroupStartData
53 FormatEntryData
1 GroupEndData
1 FormatEndData

These formating records are then vectored to an OUT-xxx command to be rendered into the appropriate data for a particular output device.  By default, they go to Out-Host but you can pipe this to Out-File, Out-Printer or Out-String.  (NOTE: these OUT-xxx commands are pretty clever, if you pipe formating objects to them, they'll render them.  If you pipe raw object to them, they'll first call the appropriate formatter and then render them.)

So to make match-string work against the string version of Monad's output, you'd do this:

MSH> get-alias | out-string -stream | match-string "ri"
Alias clv clear-variable
Alias gdr get-drive
Alias gv get-variable
Alias ndr new-drive
Alias nv new-variable
Alias rdr remove-drive
Alias ri remove-item
Alias rv remove-variable

Notice a couple things 1) you had to say "Out-String -STREAM" (if you didn't say -STREAM you would have gotten a single string) 2) now match-string matches any line that has an "ri" in it not just the ones where the NAME has an "ri" in it.

Think about the last statement 'just the ones where the NAME has an "ri" in it'. One of the things we designed Monad to do was to give you an experience where you think it, you type it, you get it. So let's say it, "I want to GET the ALIASes WHERE the NAME matches the string RI". Now lets type it:

MSH> GET-ALIAS |WHERE {$_.NAME -match "ri"}

CommandType Name Definition
----------- ---- ----------
Alias ri remove-item
Alias write write-object

It take a while to stop thinking in terms of streams of text and start thinking in terms of streams of objects.  The benefit is that once you do this, you begin to ask questions in a way that makes it really easy to answer.


Jeffrey Snover
Monad Architect