为什么这个 PowerShell 别名不起作用?

Lin*_*eak 5 powershell alias

我正在学习 PowerShell。

我想了解为什么 Windows 8.1 下 PowerShell 5.0 中的某些别名不起作用。

例如,这个命令单独工作:

Get-WmiObject -Class Win32_WinSAT
Run Code Online (Sandbox Code Playgroud)

但是在我$profile的如下定义时它不会:

Set-Alias -Name wei -Value 'Get-WmiObject -Class Win32_WinSAT'
Run Code Online (Sandbox Code Playgroud)

错误消息如下:

PS C:\> wei
wei : The term 'Get-WmiObject -Class Win32_WinSAT' 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.
At line:1 char:1
+ wei
+ ~~~
    + CategoryInfo          : ObjectNotFound: (Get-WmiObject -Class Win32_WinSAT:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
Run Code Online (Sandbox Code Playgroud)

编辑:

我看到别名的工作方式与我习惯的 Linux 上的标准 Bash 略有不同。

解决方案是简单地将其声明为函数:

Function wei { Get-WmiObject -Class Win32_WinSAT }
Run Code Online (Sandbox Code Playgroud)

ahm*_*iee 6

如果您想将其他参数传递给您的别名,您可以这样做:

function wei([Parameter(ValueFromRemainingArguments = $true)]$params) {
    & Get-WmiObject -Class Win32_WinSAT $params
}
Run Code Online (Sandbox Code Playgroud)