Shorter versions of powershell command-line options

Given my research, I do not think the following is easy to do, if at all. As a last resort, however, I decided that I would check here.

In Powershell 2.0, I would like to reduce (annoyingly) long parameter names for different cmdlets. I would like absolute control over the shortened version. (Unlike being subordinate to any abbreviation abbreviation scheme used by PS).

So, for example, I would like 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." 

The closer I can get to this functionality and how? I could not find anything about the parameter abbreviation using "get-help about_parameters".

Thanks!

+4
source share
4 answers

Check out this script: Get-Parameter.ps1

dot-source and by doing the following, it gives a ton of information about the parameters of the command. Take a look at the aliases column, it will show all the built-in parameter aliases, and also calculate the shortest name you can use for the 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 
+10
source

You can create parameter aliases for your own functions:

 function ParamAlias { param( [Alias('fg','fColor')] $ForegroundColor ) Write-Host "$ForegroundColor" -ForegroundColor $ForegroundColor } ParamAlias -fg Green ParamAlias -fColor Green 

You can 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 / parameter abbreviations in the console, and you should not use aliases in scripts, so I'm not sure if it will be worth the effort. I would go with @Shay to answer

+8
source

The parameters for this cmdlet must be sufficient to be different from this cmdlet. Things like Get-Member -m Property (-m means MemberType, which is the only "M" parameter for this cmdlet).

If I print a quick single-line box, I try to use only the first 3 characters of the parameter. This works most of the time and looks like a Cisco CLI if you have ever worked with it. From time to time, I infer a parameter outside of myself when I am debugging to make sure that I refer to the correct one.

IMO, I try not to do this in scripts. I try to make my scripts as accessible as possible to other people who may not know the aliases of each cmdlet. This helps pass on scripts to other people. If you read Don Jones's blog / article, he talks about this and that. However, if the script is just for me, I do it as short and fast as possible.

+2
source

Something like this will give you existing aliases for cmdlet parameters:

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

I really don't see a way to β€œcontrol” aliases.

0
source