CMD 和 PowerShell 中 where 命令的区别

事情的起因是我想解决在 Windows Terminal 中使用 code . 命令打开的不是 VSCode 而是 Cursor 的问题。

where 命令的介绍

在 Windows 命令提示符(CMD)中,where 命令用于查找与指定名称匹配的可执行文件的位置,它可以帮助你确定某个程序或文件在系统中的具体路径。我的使用场景是用它来查找 code 命令对应的可执行文件的路径。

现象

在 CMD 中执行 where code 命令,结果符合预期,结果如下:

CMD 中执行结果

但在我常用的 PowerShell 中执行的结果则是没有任何输出:

PowerShell 中执行结果

原因

这是因为在 PowerShell 中 where 表示的不再是 where.exe,而是 Where-Object cmdlet 的别名。而 Where-Object 的作用是根据属性值从集合中选择对象,和 Linux 中的 grep 命令作用类似,通常是结合管道一起使用的。现在直接执行 where code 命令,因为管道的上游没有任何输入,自然也不会输出任何内容。

where 命令在 PowerShell 中的结果

在 PowerShell 中使用 where 命令的正确姿势应该是使用 where.exe

在 PowerShell 中使用 where.exe

或者直接在 PowerShell 中使用 Get-Command 命令:

Get-Command 命令效果

但显然 Get-Command 命令的输出显示方式远不如 where 命令直观。

参考链接

where

Where-Object

Get-Command

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