Windows 中的“which”命令在 Unix 中的等价物是什么?是否有等效的 PowerShell 命令?

130 windows powershell command-line which

在 Linux 中,我们有“which”命令来找出可执行文件的路径。
它的 Windows 等价物是什么?是否有任何 PowerShell 命令可以执行此操作?

Ran*_*evy 129

较新版本的 Windows(我认为 Windows 2003 及更高版本)具有 where 命令:

C:\>where ping
C:\Windows\System32\PING.EXE
Run Code Online (Sandbox Code Playgroud)

对于 PowerShell,显式添加 .exe 后缀:

PS C:\>where.exe ping
C:\Windows\System32\PING.EXE
Run Code Online (Sandbox Code Playgroud)

  • 在 Powershell 中,你应该说 `where.exe ping` 因为 `where` 默认别名为 `Where-Object` cmdlet,这是完全不同的故事 (11认同)
  • 这仅适用于 cmd,不适用于 PowerShell(以我的经验) (9认同)
  • `where` 在 Windows 7 中为我工作 (6认同)
  • 在 PowerShell 中,“where.exe”显式地比“where”更适合我 (2认同)

小智 57

是的,Get-Command会找到包括可执行文件在内的所有命令:

PS\> Get-Command ipconfig
Run Code Online (Sandbox Code Playgroud)

如果要将命令限制为可执行文件:

PS\> Get-Command -CommandType Application
Run Code Online (Sandbox Code Playgroud)

会在你的路径中找到所有的exes。有一个交互使用的别名:

PS\> gcm net* -CommandType Application
Run Code Online (Sandbox Code Playgroud)

要获取可执行文件的路径,您可以使用Path返回对象的属性。例如:

PS\> (Get-Command notepad.exe).Path
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请运行man Get-Command -full


drk*_*gel 8

where.exe明确而不是where在 PowerShell 中为我工作:

PS C:\Users\birdc> where ping
PS C:\Users\birdc> where.exe ping
C:\Windows\System32\PING.EXE
Run Code Online (Sandbox Code Playgroud)