在PowerShell中运行时,为什么’where’命令不显示任何输出?

Why doesn't the 'where' command display any output when running in PowerShell?

当我在CMD中运行where时,我得到输出:

1
2
3
C:\Users
agesh> where calc
C:\Windows\System32\calc.exe

但在PS中同样的事情:

1
2
PS C:\data\code> where calc
PS C:\data\code>

输出在哪里?!


以下对我有用:

1
2
PS C:\Users\Bill> where.exe calc
C:\Windows\System32\calc.exe

在PS中键入where时,它与执行where.exe不同

1
2
3
4
5
PS C:\Users\Bill> where  [cc lang="powershell"]

cmdlet Where-Object at command pipeline position 1
Supply values for the following parameters:
Property:

因此,当您键入where calc时,它正在执行Where-Object calc(Where-Object的别名是where?),因此不返回任何内容,也不执行where.exe calc

您可以使用Get-Command(别名gcm)cmdlet而不是where.exe。 这是一个使Get-Command函数与where.exe完全相同的函数示例。 如果将其放在PowerShell配置文件中,它将始终在您的会话中可用。

1
2
3
4
function which ($command) {
    Get-Command -Name $command -ErrorAction SilentlyContinue |
        Select-Object -ExpandProperty Path -ErrorAction SilentlyContinue
}

以下链接可能有用 -

相当于* Nix''在Powershell中的命令?

https://superuser.com/questions/34492/powershell-equivalent-to-unix-which-command

希望这可以帮助。