PowerShell 中的 which 等效命令
-
使用
Get-Command作为 PowerShell 中Which命令的等效命令 -
在 PowerShell 中使用
gcm作为Which命令的等效命令 -
使用
New-Alias作为 PowerShell 中Which命令的等效命令
在 Linux 中,which 命令显示在终端提示符中输入时可以执行的指定可执行文件或命令的位置。它显示了命令的完整路径。which 命令在 PowerShell 中不可用。
这是一个在 Linux 中查看 gcc 位置的 which 命令示例。
$ which gcc
输出:
/usr/bin/gcc
PowerShell 有许多可以在其 shell 中执行的可执行文件或命令。本教程将介绍不同的方法来获取这些可执行文件或命令在 PowerShell 中的位置。它们等效于 which 命令并执行相同的任务。
使用 Get-Command 作为 PowerShell 中 Which 命令的等效命令
Get-Command cmdlet 显示计算机上安装的所有命令,包括 cmdlet、aliases、functions、filters、scripts 和 applications。它在不带任何参数的情况下打印计算机上安装的所有 cmdlet、函数和别名。
Get-Command
要查看 gcc 的位置,你可以使用以下命令。
Get-Command gcc
完整路径显示在 Source 列上。此外,它还显示 CommandType、Name 和 Version 详细信息。
输出:
CommandType Name Version Source
----------- ---- ------- ------
Application gcc.exe 0.0.0.0 C:\MinGW\bin\gcc.exe
你还可以提供多个参数。
Get-Command Write-Host, New-Alias, Describe
输出:
CommandType Name Version Source
----------- ---- ------- ------
Cmdlet Write-Host 3.1.0.0 Microsoft.PowerShell.Utility
Cmdlet New-Alias 3.1.0.0 Microsoft.PowerShell.Utility
Function Describe 3.4.0 Pester
在 PowerShell 中使用 gcm 作为 Which 命令的等效命令
gcm 是 Get-Command cmdlet 的内置别名。你可以使用 gcm 别名作为 PowerShell 中的 which 命令的等效命令。它打印与 Get-Command 相同的输出。
gcm notepad
输出:
CommandType Name Version Source
----------- ---- ------- ------
Application notepad.exe 10.0.19... C:\Windows\system32\notepad.exe
要仅获取 path,你可以像这样使用它。
(gcm notepad).Path
输出:
C:\Windows\system32\notepad.exe
使用 New-Alias 作为 PowerShell 中 Which 命令的等效命令
你还可以在 PowerShell 中定义新的自定义别名。New-Alias cmdlet 在 PowerShell 会话中创建一个新别名。退出会话或关闭 PowerShell 后,不会保存此类别名。
例如,你可以创建 which 作为 Get-Command cmdlet 的别名。
New-Alias which Get-Command
现在,你可以使用 which 命令在 PowerShell 中查看可执行文件或命令的位置。
which gcc
输出:
CommandType Name Version Source
----------- ---- ------- ------
Application gcc.exe 0.0.0.0 C:\MinGW\bin\gcc.exe
