Select-String behaves differently on how InputObject is passed - by stej

Status : 

  By Design<br /><br />
		The product team believes this item works according to its intended design.<br /><br />
		A more detailed explanation for the resolution of this particular item may have been provided in the comments section.

Sign in
to vote
ID 576354 Comments
Status Closed Workarounds
Type Bug Repros 2
Opened 7/19/2010 5:41:58 AM
Access Restriction Public


Consider this code:
$strings = ('first', 'second', 'third', 'last one')

$strings | Select-String -Pattern o | % { 'Line: ' + $_.Line }
Select-String -input $strings o | % { 'Line: ' + $_.Line }

I would expect that both calls to Select-String will return the same results.

That's not true, because in the second call $strings is converted to [string]. The conversion is done probably by joining items in array by $ofs, so "first second third last one" is passed to Select-String as InputObject. That's why the second call returns incorrect results.
Sign in to post a comment.
Posted by Aleksandar Nikolić on 7/27/2010 at 6:06 AM
It looks like Microsoft was aware of the problem, because they changed the InputObject description from V1 to V2:

V1: -inputObject <psobject>
    Accepts an object as input to the cmdlet. Enter a variable that contains the objects or type a command or expression that gets the objects.

V2: -InputObject <psobject>
     Specifies the text to be searched. Enter a variable that contains the text, or type a command or expression that gets the text.

Unfortunately, the Select-String help topic is still missing description similar to a one we can find in Get-Member help topic:

PS>Get-Help Get-Member -Parameter inputobject

-InputObject <psobject>
    Specifies the object whose members are retrieved.

    Using the InputObject parameter is not the same as piping an object to Get-Member. The differences are as follows...
Posted by stej on 7/22/2010 at 1:54 AM
Jason, I know there are some workarounds, but they are still workarounds :|

Until this bug I haven't realized that there is such a huge difference between passing value by pipeline vs. passing value as standard parameter. For me it sounds like an inconsistency that confuses me.
Posted by Jason M Archer on 7/21/2010 at 9:09 AM
stej, you could also use a foreach.
Posted by stej on 7/19/2010 at 8:55 PM
So, does it mean that it is safe to pass parameters to some cmdlets only via pipe? Is it documented anywhere?
"This is by design." says that it can happen on any cmdlet, so I should keep my eyes open :(
Posted by Microsoft on 7/19/2010 at 11:20 AM
When you pass $strings as agrument for -InputObject, it does not get unravelled.
$strings is converted to its string representation ("first second third last one")before select-string act on it. This is by design.