Shorter versions of powershell cmdlet parameters

Larold picture Larold · Aug 31, 2011 · Viewed 10.3k times · Source

Given my research, I don't believe the following is easily accomplished, if at all. As a last resort, however, I figured I'd check here.

In Powershell 2.0, I'd like a way to reduce the (annoyingly) long names of parameters to various cmdlets. I would like absolute control over what the shorthand version looks like. (As opposed to being a slave to whatever parameter abbreviation scheme PS uses.)

So, for example, I'd like to be able to do something like this:

# Command goes on this first line to alias "-ForegroundColor" to "-fg"
# Command goes on this second line to alias "-BackgroundColor" to "-bg"
Wr-te-Host -fg yellow -bg black "Parameter aliases now work just like I want."

What's the closest I can get to this functionality, and how? I was not able to find anything regarding parameter abbreviation using 'get-help about_parameters'.

Thanks!

Answer

Shay Levy picture Shay Levy · Aug 31, 2011

Check this script: Get-Parameter.ps1

dot-source it and execute the following, it gives a wealth of information about a command parameters. Take a look at the aliases column, it will show all built-in parameter aliases as well as calculates the shortest name you can use for a parameter:

PS > Get-Parameter Write-Host


    Command: Microsoft.PowerShell.Utility/Write-Host
    Set:     Default


Name                   Aliases      Position Mandatory Pipeline ByName Provider        Type
----                   -------      -------- --------- -------- ------ --------        ----
BackgroundColor        {b}          Named    False     False    False  All             ConsoleColor
ForegroundColor        {f}          Named    False     False    False  All             ConsoleColor
NoNewline              {n}          Named    False     False    False  All             SwitchParameter
Object                 {obj}        0        False     True     False  All             Object
Separator              {s}          Named    False     False    False  All             Object