windows - 相当于 BASH(等) 'type' 命令的 PowerShell?

标签 windows shell powershell powershell-2.0 command-line-interface

在 *nix 上,使用 BASH(等)你通过使用内置的“type”shell 询问系统命令所在的位置(等),如下所示:

$ type cat
cat is /bin/cat

Microsoft PowerShell 2.0 中是否有与此“type”命令等效的命令?

最佳答案

一个等价物是 Get-Command

PS C:\> Get-Command ls

CommandType     Name       Definition
-----------     ----       ----------
Alias           ls         Get-ChildItem
Application     ls.exe     D:\usr\local\wbin\ls.exe
Application     ls.exe     C:\Program Files (x86)\Git\bin\ls.exe

Windows 10 更新:

自从我发布了这个答案后,Get-Command 的行为似乎发生了变化。要包含所有结果(以 Un*x 的样式 type),现在我需要传递 -All 标志,如下所示:

PS C:\> Get-Command -All ls

CommandType     Name                 Version    Source
-----------     ----                 -------    ------
Alias           ls -> Get-ChildItem
Application     ls.exe               0.0.0.0    C:\Program Files (x86)\Git\usr\bin\ls.exe

如评论中所述,此像以前的行为一样包含Definition 列。我无法确定添加定义列的命令行参数,但正如@voutasaurus 在下面的评论中指出的那样,可以使用:

PS C:\> (Get-Command -All ls).Definition
Get-ChildItem
C:\Program Files (x86)\Git\usr\bin\ls.exe

供引用的版本信息(我没有原始答案文本相关的版本信息,但我猜它是Windows 7):

PS C:\> [System.Environment]::OSVersion.Version

Major  Minor  Build  Revision
-----  -----  -----  --------
10     0      15063  0

关于windows - 相当于 BASH(等) 'type' 命令的 PowerShell?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12320682/

相关文章:

arrays - 从 powershell 数组/集合/哈希表中删除一行

powershell - 为什么 PowerShell 脚本在提示输入时会暂停?

c++ - mingw g++ 无法与库链接

Linux:tail -f 多个选项

c# - 如何终止在 C# 中的 foreach 循环中启动的线程

bash - 在 unix/linux shell 中进行模式匹配时如何使用反向或负通配符?

bash - 文件 Bash 意外结束(最后一行)

powershell - 无法连接到服务器: dial tcp [::1]:8080: connectex: 由于目标机器主动拒绝而无法建立连接

windows - 混合来自不同版本的 Visual Studio 的库时的运行时问题

windows - 如何使用GetStorageFileName函数将训练文件添加到注册表?