InputObject is a generic name used for a parameter that takes pipeline input. It's part of internal PowerShell naming convention and there is nothing special about it.
Your example doesn't work as you think it should, because when you pass a collection to the InputObject parameter it's treated as a single item and not unwrapped to individial elements, so it doesn't get sorted. This allows you to sort a collection of collections.
Consider this examples:
This is how Sort-Object works:
function Add-Quotes
{
Param
(
[Parameter(ValueFromPipeline = $true)]
$InputObject
)
Process
{
"'$InputObject'"
}
}
Note that array is automatically unwrapped by the pipeline, then idividial items are assigned the $InputObject variable in each iteration and then processed in Process block:
PS> $items | Add-Quotes
'a'
'b'
'c'
'd'
But when you pass a collection to the InputObject it's not iterated over, because there is no pipeline to unwrap it:
PS> Add-Quotes -InputObject $items
'a b c d'
Sometimes it's a desired behavior, sometimes you need to unwrap collections no matter where they came from. In this case you use internal foreach loop to do so:
function Add-Quotes
{
Param
(
[Parameter(ValueFromPipeline = $true)]
[string[]]$InputObject
)
Process
{
foreach($item in $InputObject)
{
"'$item'"
}
}
}
PS > $items | Add-Quotes
'a'
'b'
'c'
'd'
PS > Add-Quotes -InputObject $items
'a'
'b'
'c'
'd'
Hope this makes it clear for you.
Sort-Object(by design) doesn't work when you bind your input collection to-InputObjectby name - you need to pipe your values toSort-Object, at which point they're automatically bound to theInputObjectparameter. In that sense, all the examples in the help page shows the (implicit) use of-InputObject$items |sort -Descending. Seehelp sort -parameter inputobject$items |sort -Descendingexpression works. Also I have read the documentation before I created this theme. I want to get the example of-InputObjectparameter using. In my code example I got not the same result that I expected. Your example don't use that parameter.Trace-Command -Name ParameterBinding -Expression {1,2|sort} -PSHostto see what's actually going on with your input