在 PowerShell 上设置默认 CLI 选项的别名

Alias to set default CLI options on PowerShell

我想使用 new-aliasset-alias 别名将 --includeuser 设置为 get-process cmdlet 的默认开关。

我愿意:

new-alias get-process 'get-process -includeuser'

但是我在执行 get-process 时收到以下错误:

get-process : The term 'get-process -includeuser $*' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

请帮助我。


为了比较 cmd.exe 可以使用 doskey 设置别名,如下所示:

doskey task=tasklist
如果命令有输入参数,则可以使用

$*

PowerShell 中的别名不接受参数。通常的解决方法是定义一个短函数;例如:

function task { tasklist $args }

$args 变量大致相当于 doskey 中的 $*

在 PowerShell v3 及更高版本中,您还可以使用内置的 $PSDefaultParameterValues 变量(哈希表)为 cmdlet 分配默认参数值。例如:

$PSDefaultParameterValues.Add("Get-Process:IncludeUserName",$true)

这将指定 Get-Process cmdlet 默认使用 -IncludeUserName

哈希表键是 cmdlet 名称、: 字符和参数名称(不带前导 -),哈希表值是参数的值。