3

I am trying to pipe an array of strings to write-host and explicitly use $_ to write those strings:

'foo', 'bar', 'baz' | write-host $_

However, it fails with:

The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input.

This error message makes no sense to me because I am perfectly able to write

'foo', 'bar', 'baz' | write-host

I would have expected both pipelines to be equivalent. Apparently, they're not. So, what's the difference?

René Nyffenegger
  • 37,844
  • 31
  • 151
  • 262
  • $_ is the current object for cmdlet's like `ForEach-Object`, like: `'foo', 'bar', 'baz' | ForEach-Object {write-host $_}` – iRon Apr 13 '19 at 12:50

2 Answers2

3

I would have expected both pipelines to be equivalent.

They're not:

'foo', 'bar', 'baz' | write-host

It is the pipeline-based equivalent of the following (equivalent in ultimate effect, not technically):

foreach ($str in 'foo', 'bar', 'baz') { Write-Host -Object $str }

That is, in your command Write-Host receives input from the pipeline that implicitly binds to its -Object parameter for each input object, by virtue of parameter -Object being declared as accepting pipeline input via attribute [Parameter(ValueFromPipeline=$true)]


'foo', 'bar', 'baz' | write-host $_

Before pipeline processing begins, arguments - $_ in your case - are bound to parameters first:

Since $_ isn't preceded by a parameter name, it binds positionally to the - implied - -Object parameter.

Then, when pipeline processing begins, pipeline parameter binding finds no pipeline-binding Write-Host parameter to bind to anymore, given that the only such parameter, -Object has already been bound, namely by an argument $_.

In other words: your command mistakenly tries to bind the -Object parameter twice; unfortunately, the error message doesn't exactly make that clear.

The larger point is that using $_ only ever makes sense inside a script block ({ ... }) that is evaluated for each input object.
Outside that context, $_ (or its alias, $PSItem) typically has no value and shouldn't be used.

While $_ is most typically used in the script blocks passed to the ForEach-Object and Where-Object cmdlets, there are other useful applications, most typically seen with the Rename-Item cmdlet: a delay-bind script-block argument:

# Example: rename *.txt files to *.dat files using a delay-bind script block:
Get-Item *.txt | Rename-Item -NewName { $_.BaseName + '.dat' }

That is, instead of passing a static new name to Rename-Item, you pass a script block that is evaluated for each input object - with the input object bound to $_, as usual - which enables dynamic behavior.

As explained in the linked answer, however, this technique only works with parameters that are both (a) pipeline-binding and (b) not [object] or [scriptblock] typed; therefore, given that Write-Object's -Object parameter is [object] typed, the technique does not work:

 # Try to enclose all inputs in [...] on output.
 # !! DOES NOT WORK.
 'foo', 'bar', 'baz' | write-host -Object { "[$_]" }

Therefore, a pipeline-based solution requires the use of ForEach-Object in this case:

# -Object is optional
PS> 'foo', 'bar', 'baz' | ForEach-Object { write-host -Object "[$_]" }
[foo]
[bar]
[baz]
mklement0
  • 312,089
  • 56
  • 508
  • 622
0

You can use it as iRon has indicated in the comments. $_ or $PSItem is the current object in the pipeline that is being processed. Typically, you see this with commands that require a processing or script block. You would have to contain your Write-Host command within a similar processing block.

'foo', 'bar', 'baz' | ForEach-Object {write-host $_}

Here is an example using the process block of a function:

function write-stuff {
    process { write-host $_ }
}

'foo', 'bar', 'baz' | write-stuff
bar
foo
hi
AdminOfThings
  • 22,344
  • 3
  • 12
  • 21