030
030

Reputation: 11669

How to alias a Powershell function?

The aim is to call the function hello by calling hello or alias helloworld

Code:

function hello() {
param(
  [string] $name
)
  Write-Host "Hello $name!"
}

hello "Utrecht"
helloworld "Utreg"

Expected outcome:

Hello Utrecht!
Hello Utreg!

Upvotes: 20

Views: 18009

Answers (3)

alroc
alroc

Reputation: 28144

Use the set-alias cmdlet.

set-alias -name helloworld -value hello

It should be noted though that your function name does not follow the PowerShell convention and may be confusing to someone more accustomed to using PowerShell.

Upvotes: 11

Samselvaprabu
Samselvaprabu

Reputation: 18147

You can also add Alias After function but before param as below

function hello() {
[alias("HelloWorld")]
   param(
     [string] $name
   )
  Write-Host "Hello $name!"
}

Helloworld
Hello

It will also create Alias name. Updated:

When alias is created this way, it is not globally exported. So if you are using this alias from another script file, please use Set-Alias

Upvotes: 39

Bill_Stewart
Bill_Stewart

Reputation: 24525

Use an alias?

set-alias helloworld hello

Upvotes: 2

Related Questions