Search code examples
shellpowershellpowershell-2.0piping

Why is `help format-list` not the same as `format-list | help`?


help format-list outputs the help for format-list.

format-list | help outputs the help for help (get-help).


Solution

  • Help is a function that essentially redirects to:

    Get-Help command | more
    

    If you look at this function's definition, you will see that it accepts a positional argument tagged with ValueFromPipelineByPropertyName with an argument of Name.

    PS ~\> Get-Content function:help
    
    <#
    .FORWARDHELPTARGETNAME Get-Help
    .FORWARDHELPCATEGORY Cmdlet
    #>
    [CmdletBinding(DefaultParameterSetName='AllUsersView')]
    param(
        [Parameter(Position=0, ValueFromPipelineByPropertyName=$true)]
        [System.String]
        ${Name},
    
        # Other arguments deleted for brevity
    
        [Switch]
        ${Online})
    $outputEncoding=[System.Console]::OutputEncoding
    
          Get-Help @PSBoundParameters | more
    

    This basically means, if it sees an argument with a property called Name, it binds that as an input parameter. So, when you do:

    format-list | help
    

    the format-list command is run (and nothing is returned), so the help function thinks it received no arguments.

    When you do:

    "format-list" | help
    

    You are passing a string argument. The string type does not have a Name prooperty, so you'll get an error message saying that it can't bind the arguments. However, if you tried:

    PS ~\> get-command format-list
    
    CommandType     Name                Definition
    -----------     ----                ----------
    Cmdlet          Format-List         Format-List [[-Property] <Object[]>] [-GroupBy <...
    

    you can see the the command format-list does have a Name property, so if you tried

    get-command format-list | help
    

    You get the help for format-list.