PowerShell中等同于BASH(等)'type'命令的命令是什么?

12
在*nix系统中,使用BASH(等)时,可以通过使用“type” shell内置命令来询问系统命令的位置(等),方式如下:
$ type cat
cat is /bin/cat

在 Microsoft PowerShell 2.0 中有类似于 'type' 命令的等效命令吗?

4个回答

15
一个等价的命令是“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 类似),现在需要传递 -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

这对于在$profile中定义的函数无效,而type返回bash中函数的定义。 - voutasaurus
我得到了这个:> Get-Command Get-Command 命令类型 名称 版本 来源 ----------- ---- ------- ------ Cmdlet Get-Command 3.0.0.0 Microsoft.PowerShell.Core如果你使用(Get-Command Get-Command).Definition,那么你实际上会得到定义。 - voutasaurus
谢谢@voutasaurus,自回答发布以来可能已经发生了变化。我已经采纳了您的建议并修改了答案。 - pb2q

2
  • Get-Command 看起来不错,别名为 gcm
  • 只需将其管道传递到 Select-Object *,别名为 select *

例如,在 $profile 中,我有一个函数,它从 pwd 打开 total commander(它会先静默地杀死现有实例)

function start-totalCommanderhere {
    $here = (Get-Location).path
    kill -n TOTALCMD64 -ErrorAction Ignore
    start "c:\totalcmd\TOTALCMD64.EXE" $here
}
Set-Alias tc start-totalCommanderhere

以下是关于该函数的所有信息——但它并没有直接告诉你它是一个函数,你需要查询它的正确名称而不是别名,并从CommandType属性中获取这些信息。(然后它就像bash中的type一样,在情况下它是一个函数时告诉你hello_world是一个函数
▶ gcm tc | select *

HelpUri             :
ResolvedCommandName : start-totalCommanderhere
DisplayName         : tc -> start-totalCommanderhere
ReferencedCommand   : start-totalCommanderhere
ResolvedCommand     : start-totalCommanderhere
Definition          : start-totalCommanderhere
Options             : None
Description         :
OutputType          : {}
Name                : tc
CommandType         : Alias
Source              :
Version             :
Visibility          : Public
ModuleName          :
Module              :
RemotingCapability  : PowerShell
Parameters          : {}
ParameterSets       :
  • gcm <module_name> | select * 如果你要求一个 CommandTypeApplication,它会给你路径。
 gcm ping | select *

HelpUri            :
FileVersionInfo    : File:             C:\Windows\system32\PING.EXE
                     InternalName:     ping.exe
                     OriginalFilename: ping.exe.mui
                     FileVersion:      10.0.19041.320 (WinBuild.160101.0800)
                     FileDescription:  TCP/IP Ping Command
                     Product:          Microsoft® Windows® Operating System
                     ProductVersion:   10.0.19041.320
                     Debug:            False
                     Patched:          False
                     PreRelease:       False
                     PrivateBuild:     False
                     SpecialBuild:     False
                     Language:         English (United States)

Path               : C:\Windows\system32\PING.EXE
Extension          : .EXE
Definition         : C:\Windows\system32\PING.EXE
Source             : C:\Windows\system32\PING.EXE
Version            : 10.0.19041.1
Visibility         : Public
OutputType         : {System.String}
Name               : PING.EXE
CommandType        : Application
ModuleName         :
Module             :
RemotingCapability : PowerShell
Parameters         :
ParameterSets      :
  • 但是使用gcm时,您无法获取导入模块的路径 - 您需要使用Get-Module | Select-Object Name, Path作为别名gmo | select name,path
▶ gmo | select name, path

Name                            Path
----                            ----
assign-vault-keys-to-env-vars   C:\Users\Admin\Documents\workspace\work.log\kb\powershell\assign-vault-keys-to-env-vars.ps1
CimCmdlets                      C:\Program Files\PowerShell\7\Microsoft.Management.Infrastructure.CimCmdlets.dll
decode-base64                   C:\Users\Admin\Documents\workspace\work.log\kb\powershell\decode-base64.ps1
DnsClient                       C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\DnsClient\DnsClient.psd1
...

2
Get-Command有一个-ShowCommandInfo参数可以做到这一点。它也适用于在$profile中定义的函数:
PS C:\Users\vp937ll> Get-Command l -ShowCommandInfo

Name          : l
ModuleName    :
Module        : @{Name=}
CommandType   : Function
Definition    : Get-ChildItem | Sort-Object -Property LastWriteTime -Descending
ParameterSets : {@{Name=__AllParameterSets; IsDefault=False; Parameters=System.Management.Automation.PSObject[]}}

1

既然您将此标记为Shell,除了PowerShell的Get-Command之外,还有where.exe

PS C:\> where.exe notepad
C:\Windows\System32\notepad.exe
C:\Windows\notepad.exe

该命令只是通过路径查找具有指定名称的文件。
PS C:\> where.exe readme.*
C:\Python31\README.txt
C:\Program Files (x86)\wget\README
C:\Program Files (x86)\SysinternalsSuite\readme.txt

请注意,在从PowerShell调用此命令时,您必须将其称为where.exe,因为Where-Object被别名为where

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接