20

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!
3
  • 3
    What's wrong with set-alias? Commented Jul 10, 2014 at 18:14
  • 2
    While Set-Alias works, sometimes you don't want it to be global. Commented Mar 11, 2020 at 16:07
  • 1
    That's what the -Scope parameter is for. Set-Alias -Name 'HelloWorld' -Value 'Hello' -Scope Script Commented Feb 10, 2023 at 18:29

3 Answers 3

39

You can also add alias after the function, but before param, as below:

function hello() {
    [alias("HelloWorld")]
    param(
        [string] $name
    )

    Write-Host "Hello $name!"
}

Output:

Helloworld
Hello

It will also create an alias name.

When an 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.

Sign up to request clarification or add additional context in comments.

1 Comment

This is what I was looking for thanks. I knew about Set-Alias already but this keeps it tidy.
11

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.

Comments

2

Use an alias:

set-alias helloworld hello

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.