In Powershell, a pipeline can contain filters such as ForEach-Object:
get-process | %{$_.name}
Within the script block of the foreach filter, it's possible to use the $_
auto variable to refer to the current object of the script block.
What does the $_
variable bind to when used at the top level of the pipeline?
For example, the following doesn't work:
"common" | get-verb -group $_
I would have thought that it was bound to the resulting object from the previous section of the pipeline -- in the above case, to the "common" string.
I've been looking online for info about how this $_
is bound, but haven't found that type of info. Virtually all examples that I see use $_
within script blocks.
For example, this doesn't answer this question: What does $_ mean in PowerShell?
$_ is only valid inside scriptblocks. Note that -group is a powershell 7 only parameter. You can do something like this, but you have to specify something for the verb: