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

Rag*_*esh 11 powershell

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

C:\Users\Ragesh> where calc
C:\Windows\System32\calc.exe
Run Code Online (Sandbox Code Playgroud)

但在PS中同样的事情:

PS C:\data\code> where calc
PS C:\data\code>
Run Code Online (Sandbox Code Playgroud)

输出在哪里?!

Bil*_*ill 13

以下对我有用:

PS C:\Users\Bill> where.exe calc
C:\Windows\System32\calc.exe
Run Code Online (Sandbox Code Playgroud)

键入wherePS时,它与执行不同where.exe

PS C:\Users\Bill> where  <press ENTER>

cmdlet Where-Object at command pipeline position 1
Supply values for the following parameters:
Property:
Run Code Online (Sandbox Code Playgroud)

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

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

function which ($command) {
    Get-Command -Name $command -ErrorAction SilentlyContinue | 
        Select-Object -ExpandProperty Path -ErrorAction SilentlyContinue
}
Run Code Online (Sandbox Code Playgroud)

以下链接可能有用 -

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

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

希望这可以帮助.

  • 谢谢你的提示。但它并没有解释为什么“where”在 PS 中没有显示任何输出。 (2认同)
  • 您也可以使用cmd.exe运行它.像`cmd/c",其中calc"` (2认同)
  • 您可以通过发出`get-command where -CommandType all`来查看导致此命令的命令优先级. (2认同)