在PowerShell中设置具有属性的别名

时间:2013-09-23 23:11:37

标签: powershell

我想为目录中的列出文件设置别名,但Set-Alias -name lf -value ls -file似乎不起作用。我打算用Unix别名的方式。

3 个答案:

答案 0 :(得分:7)

别名不能这样做。来自Set-Alias的帮助:

  

您可以为cmdlet创建别名,但不能为包含cmdlet及其参数的命令创建别名。

但是,使用一种称为“splatting”的技术,函数可以轻松完成:

function lf {
  ls -file @args
}

有关详细信息,请参阅help about_splatting

答案 1 :(得分:0)

Get-Help Set-Alias -Full中的示例5是您想要的:

Function lsfile {Get-Childitem -file}
Set-Alias lf lsfile

答案 2 :(得分:0)

附加@ mike-z的答案。

您可以将函数定义放入PowerShell配置文件中,以便可以重复使用它再次打开shell。

test-path $profile
// Ensure it doesn't exists before creating the profile!
new-item -path $profile -itemtype file -force
notepad $profile

只需将代码放入文件中:

function lf { ls -file @args }

您可以查看official documentation的详细信息。