In Powershell 5, what is the difference between "help" and "get-help"?

Viewed 344

I'm relatively new to powershell, and I find the typesystem to be a little squishier than I would like. This one has puzzled me, and confused me a few times now.

In this case, I was trying to list the available cmdlets, and avoid the cursed "..." truncation.

Help:

PS C:\src\inst> help new-netipsecq* | format-table -AutoSize

Name                              Category  Module                    Synopsis
----                              --------  ------                    --------
New-NetIPsecQuickModeCryptoPro... Cmdlet    NetSecurity               New-NetIPsecQuickModeCryptoProposal...
New-NetIPsecQuickModeCryptoSet    Function  NetSecurity               ...

vs. Get-Help

PS C:\src\inst> get-help new-netipsecq* | format-table -AutoSize

Name                                Category Module      Synopsis
----                                -------- ------      --------
New-NetIPsecQuickModeCryptoProposal Cmdlet   NetSecurity New-NetIPsecQuickModeCryptoProposa...
New-NetIPsecQuickModeCryptoSet      Function NetSecurity ...

So, with get-member we can see:

PS C:\src\inst> help new-* | get-member
TypeName: System.String
Name             MemberType            Definition
----             ----------            ----------    

vs. get-help

PS C:\src\inst> get-help new-* | get-member
   TypeName: HelpInfoShort
Name          MemberType   Definition
----          ----------   ----------

Questions:

  1. Why is help different than get-help? I've checked, help is not an alias.

  2. Why does help return a string formatted to look like what get-help returns?

Hm...I found part of the answer myself. I had tried get-alias help with no luck, but now I've stumbled on 'get-command'

PS C:\src\inst> Get-Command help

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Function        help
  1. Where does this function come from?
2 Answers

help is a built-in function that is essentially a wrapper around the Get-Help cmdlet in order to provide interactive paging.

  • To discover the difference, use the Get-Command cmdlet: Get-Command help vs.
    Get-Command Get-Help

  • To see the body of the help function, run $function:help or Get-Content Function:help


Behind the scenes, the help function uses the Out-String cmdlet in order to get the same representation that you would see in the PowerShell console, as a string, which is the prerequisite for using the external more.com executable for paging (less on Unix), which only understands string input - and not the formatting objects that PowerShell - either implicitly or explicitly via Format-* cmdlets - uses for output formatting.

more.com / less also output only strings (and automatically disable paging when not outputting to a terminal (console), essentially passing the input string through), so applying a Format-* cmdlet is pointless, because it passes strings through as-is (only non-primitive objects are subject to formatting).

In other words, the | format-table -AutoSize part of
help new-netipsecq* | format-table -AutoSize is a no-op.

However, even with Get-Help, applying a Format-* cmdlet is pointless if you're unambiguously targeting a single topic, because Get-Help itself provides proper output formatting for help topics.

Your sample commands use a wildcard expression (new-netipsecq*) to match multiple topics, which causes Get-Help to display a table listing those topics rather than rendering those topics themselves; such output can be meaningfully modified with Format-* cmdlets.

Note that this listing can even occur with a literal name, namely if there - unexpectedly - happen to be multiple conceptual topics with the very same name present. In that case, a workaround is needed to force rendering of those topics themselves - this answer.

Answering 1 and 2:

This shows what is going on for why there is a difference... (get-command help).definition

Help is like get-help %* | more

(Please, forgive the batch syntax!)

Related