How to alias a Powershell function?

孤者浪人 提交于 2021-01-27 07:33:02

问题


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!

回答1:


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.




回答2:


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




回答3:


Use an alias?

set-alias helloworld hello


来源:https://stackoverflow.com/questions/24683539/how-to-alias-a-powershell-function

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