9

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!

Larold
  • 640
  • 2
  • 8
  • 19
  • 7
    You only have to type enough letters of a parameter to disambiguate it: write-host -f green -b blue hello! – x0n Sep 01 '11 at 15:50
  • 2
    @Larold, I think there's a typo in your code snippet. I can't fix it because it's just 1 char (min 6 to edit). – gsscoder Mar 16 '20 at 10:06

4 Answers4

18

You can create parameter aliases for your own functions like so:

function ParamAlias {
    param(
        [Alias('fg','fColor')]
        $ForegroundColor
    )

    Write-Host "$ForegroundColor" -ForegroundColor $ForegroundColor
}

ParamAlias -fg Green
ParamAlias -fColor Green

You could then use this technique with Proxy CmdLets to add your own aliases to existing CmdLets. However, I find it sufficient to use existing parameter aliases/shortened parameter names in the console, and you shouldn't use aliases in scripts, so I'm not sure this would be worth the effort. I would go with @Shay's answer

Rynant
  • 23,153
  • 5
  • 57
  • 71
14

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
doubleDown
  • 8,048
  • 1
  • 32
  • 48
Shay Levy
  • 121,444
  • 32
  • 184
  • 206
2

Something like this would give you the existing aliases for a cmdlet's parameters:

Get-Command write-host |
    ForEach-Object {$_.parameters |
        ForEach-Object { $_.Values |
            Where-Object {
                $_.Aliases.Count -gt 0 } |
                Select-Object Name, Aliases
            }
    }

I don't really see a way for "controlling" the aliases though.

manojlds
  • 290,304
  • 63
  • 469
  • 417
  • That's because in general they aren't aliases. It is an adaptive disambiguation algorithm. In advanced functions that you write yourself you *can* specify aliases for parameters, but even without specifying anything they will still disambiguate on their own. – EBGreen Aug 31 '11 at 20:58
  • 1
    @EBGreen - Sorry, I don't get your point. As per OP's question, `-ForeGroundColor` needs to have an *alias* `fg` whereas `f`, `fo`, `for` etc. are used for disambiguation. Is that the same point you are making? – manojlds Aug 31 '11 at 21:00
  • Meh, I wasn't being clear. Nothing new there. I guess I was trying to make the point that what most people think are aliases to the parameters are really just the disambiguation algorithm. As your code shows there are actual aliases also. I guess the take away from my comment should be that a lot of what people often think are aliases really aren't; You can't add aliases on built-in cmdlets; You can create aliases on your own functions/cmdlets. – EBGreen Aug 31 '11 at 21:05
  • @EBGreen - Ok makes more sense now :) – manojlds Aug 31 '11 at 21:07
2

Parameters of a given cmdlet only need to have enough of it to make it distinct within that cmdlet. Things like Get-Member -m Property (-m stands for MemberType, which is the only "M" parameter for this cmdlet).

If I am typing out a quick one-liner I tend to use only the first 3 characters of the parameter. This works most of the time and is similar to Cisco's CLI if you ever worked with that. Every now and then I will tab the parameter out when I am debugging, to make sure I am referencing the right one.

IMO, I try not to do it to much in scripts. I try to make my scripts as readable as I can for other folks that may not know the aliases of every cmdlet. It helps in passing on scripts to other people. If you read Don Jones blog/articles he talks about this some too. However, if the script is just for me I make it as short and quick as possible.