Reputation: 1945
I want to be able to type -nm
or better still nm
and have Powershell understand it as -Name
to make entering commands etc easier.
I cannot understand the Microsoft documentation on this - it seems like absolute nonsense:
[Alias("UN","Writer","Editor")]
[Parameter()]
public string UserName
{
get { return userName; }
set { userName = value; }
}
private string userName;
What does that code mean??? What has been aliased? Does that mean that using this snippet I can now use '-UN' instead of '-UserName'? Do I have to re-define each Parameter in order to be able to alias it?
Upvotes: 0
Views: 114
Reputation: 2676
I will give you a simple trick.
Open a PowerShell ISE, Go to Edit -> Start Snippets
And choose the cmdlet (Advanced Function) - Complete
It would look like this:
This is a good place to see all you can do with Functions and parameters. What you are looking for is right here. This is how you create an Alias. Don't get mad for throwing more intimidating code at you. Allow me to explain.
Here is a bare minimum of what you will need. That's right, just the [Alias("p1")
part right before the param
is defined is all you need.
function My-Function
{
Param
(
[Alias("nm")]
$Name
)
Write-Host "Param suppplied $Name"
}
Upvotes: 1