Monday, March 25, 2013

To alias or not to alias.

To alias, or not to alias, or to imply an alias (or Function)

The difference between Help and Get-Help was raised in the discussion portion of this months Saturday PowerShell SIG.  I had always assumed that Help was just an alias for Get-Help, and was happily ignorant.

Help Vs. Get-Help


This isn't really an alias, it is a function that can be seen with:
get-item function:Help | select -expand ScriptBlock

For example a PS3.0 install with the PSCX  module installed gives:

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
[CmdletBinding(DefaultParameterSetName='AllUsersView', HelpUri='http://go.microsoft.com/fwlink/?LinkID=113316')]
param(
[Parameter(Position=0, ValueFromPipelineByPropertyName=$true)]
[System.String]
${Name},

[System.String]
${Path},

[ValidateSet('Alias','Cmdlet','Provider','General','FAQ','Glossary','HelpFile','ScriptCommand','Function','F
ilter'
,'ExternalScript','All','DefaultHelp','Workflow')]
[System.String[]]
${Category},

[System.String[]]
${Component},

[System.String[]]
${Functionality},

[System.String[]]
${Role},

[Parameter(ParameterSetName='DetailedView', Mandatory=$true)]
[Switch]
${Detailed},

[Parameter(ParameterSetName='AllUsersView')]
[Switch]
${Full},

[Parameter(ParameterSetName='Examples', Mandatory=$true)]
[Switch]
${Examples},

[Parameter(ParameterSetName='Parameters', Mandatory=$true)]
[System.String]
${Parameter},

[Parameter(ParameterSetName='Online', Mandatory=$true)]
[switch]
${Online},

[Parameter(ParameterSetName='ShowWindow', Mandatory=$true)]
[switch]
${ShowWindow}
)

$outputEncoding=[System.Console]::OutputEncoding

if ($Pscx:Preferences["PageHelpUsingLess"])
{
    Get-Help @PSBoundParameters | less
}
else
{
    Get-Help @PSBoundParameters | more
}


Without PSCX in PowerShell 2.0 the same Command generates:
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
<# .FORWARDHELPTARGETNAME Get-Help .FORWARDHELPCATEGORY Cmdlet #>
[CmdletBinding(DefaultParameterSetName='AllUsersView')]
param(
    [Parameter(Position=0, ValueFromPipelineByPropertyName=$true)]
    [System.String]
    ${Name},

    [System.String]
    ${Path},

    [System.String[]]
    ${Category},

    [System.String[]]
    ${Component},

    [System.String[]]
    ${Functionality},

    [System.String[]]
    ${Role},

    [Parameter(ParameterSetName='DetailedView')]
    [Switch]
    ${Detailed},

    [Parameter(ParameterSetName='AllUsersView')]
    [Switch]
    ${Full},

    [Parameter(ParameterSetName='Examples')]
    [Switch]
    ${Examples},

    [Parameter(ParameterSetName='Parameters')]
    [System.String]
    ${Parameter},

    [Switch]
    ${Online})
$outputEncoding=[System.Console]::OutputEncoding

      Get-Help @PSBoundParameters | more


Help is not an alias, it is a function in both installs.  People treat these as interchangeable in many online references that I have seen.  For most part they are, but they are not the 'same' and this distinction may impact learning.

Alias Ambiguity 

We also talked about where alias are helpful and a hindrance   The general idea being that alias make life easier on you, and potentially more difficult on someone else.  Specifically that someone else would be those reading your script, forum post, or forum answers.  Potentially to someone new to PowerShell this can start with confusion.

For Example:

Get-Alias -Definition Get-ChildItem

CommandType     Name                                               ModuleName                                    
-----------     ----                                               ----------                                    
Alias           dir -> Get-ChildItem                                                                              
Alias           gci -> Get-ChildItem                                                                              
Alias           ls -> Get-ChildItem



If you mix these alias with full cmdlets you can have 4 completely correct answers.  If you are trying to learn the Verb-Noun pairing of PowerShell you could completely miss it. The Verb-Noun pairs are also often referenced as key learning and exploring benefit of PowerShell.

So what could we do to completely overreact to this information?

  1. We could wipe out all of alias:\* and all function:\* that don't contain a hyphen (-).  We would probably not be able to run many other scripts though.  Not sure what this would break actually.  I could only see it as really helping in a high-wire style class room setting.
  2. Run a Function to open a PS1, or ISE and expand aliases.  Something like: http://PoshCode.org/embed/2980 
  3. Improve visibility into what you write and echo an expanded version of the last thing you typed in expanded form.  While at it, expanding any ordered or trimmed argument names would be good too (not show below).
001
002
003
004
005
006
007
008
009
# Generic append to existing Prompt Line
Copy-item Function:Prompt Function:Prompt_Backup

Function Prompt {
Write-host `n
# Expand Alias from http://PoshCode.org/embed/2980
Expand-Alias -script (get-history -count 1 ) | write-host
Function:Prompt_Backup
}