liveBook · Manning

Alias

As explained in Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools

An alias is an alternate name that invokes a PowerShell command.

An alias is an alternate name that invokes a PowerShell command. Console aliases can save typing, but scripts should favor full command and parameter names so shared code remains readable. section 17.1.1 section 18.2

Use aliases differently depending on where the code will live:

  • Use console shortcuts when speed matters and the command is only for you. section 18.2
  • Avoid ambiguous or obscure aliases in scripts; spell out commands such as Get-Service, Invoke-Command, and Get-WmiObject. section 18.6
  • Use tab completion to enter full names with less effort and reduce spelling mistakes. section 18.2

Parameter aliases

Advanced functions can assign several alternate names to one parameter. A parameter declared as [Alias('CN','MachineName','Name')] [string[]]$ComputerName accepts -CN, -MachineName, -Name, or the canonical -ComputerName. section 11.1.4

[CmdletBinding()]
Param(
    [Parameter(ValueFromPipeline=$True,
               Mandatory=$True)]
    [Alias('CN','MachineName','Name')]
    [string[]]$ComputerName,
    [string]$LogFailuresToPath,
    [ValidateSet('Wsman','Dcom')]
    [string]$Protocol = "Wsman",
    [switch]$ProtocolFallback
)

Inspect a complete advanced-function parameter block combining a parameter alias with pipeline input, validation, and a switch parameter. section 11.1.4

How Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools covers it

How deep it goes
Defines alternate names, demonstrates three parameter aliases, and stops short of parameter sets and dynamic parameters.
Examples
Uses -CN, -MachineName, and -Name for -ComputerName, and contrasts echo, icm, gwmi, and Where with full command names.
What it assumes
Builds on PowerShell commands, parameters, and functions, then places aliases within advanced-function and scripting practices.
What you can do after
Apply aliases in PowerShell consoles and define parameter aliases while choosing explicit names for maintainable scripts.
How it connects
Connects aliases to parameters, advanced functions, readability, tab completion, source control, and PSScriptAnalyzer.

Also explained in

Compare how 6 books explain alias →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools. The section links point to the parts of the book it draws on.