10

在 *nix 上,使用 BASH(等),您可以使用内置的 'type' shell 询问系统命令所在的位置(等),如下所示:

$ type cat
cat is /bin/cat

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

4

4 回答 4

14

一个等价物是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
于 2012-09-07T15:06:10.850 回答
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[]}}
于 2020-06-20T03:36:05.360 回答
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.

于 2018-01-01T22:21:17.523 回答
1
  • Get-Command似乎正确,别名为gcm
  • 只需通过管道将其Select-Object *别名为select *

例如,在$profile我有一个从 pwd 打开总指挥官的功能(它首先默默地杀死现有实例)

function start-totalCommanderhere {
    $here = (Get-Location).path
    kill -n TOTALCMD64 -ErrorAction Ignore
    start "c:\totalcmd\TOTALCMD64.EXE" $here
}
Set-Alias tc start-totalCommanderhere
  • 这是有关该函数的所有信息 - 但是它不会直接告诉您它是一个函数,您需要查询它的正确名称而不是别名才能从CommandType属性中获取该信息。type(这与bash 中的相同hello_world is a function,如果它是一个,它会告诉你)
▶ 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 *如果您要求CommandType哪个是,则为您提供路径Application
▶ 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
...
于 2021-10-16T08:06:07.017 回答