Shorter versions of powershell cmdlet parameters

♀尐吖头ヾ 提交于 2019-12-04 09:32:31
Shay Levy

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

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

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!