【发布时间】:2012-09-01 11:44:46
【问题描述】:
在 *nix 上,使用 BASH(等),您可以使用内置的 'type' shell 询问系统命令所在的位置(等),如下所示:
$ type cat
cat is /bin/cat
在 Microsoft PowerShell 2.0 中是否有与此“类型”命令等效的命令?
【问题讨论】:
标签: windows shell powershell powershell-2.0 command-line-interface
在 *nix 上,使用 BASH(等),您可以使用内置的 'type' shell 询问系统命令所在的位置(等),如下所示:
$ type cat
cat is /bin/cat
在 Microsoft PowerShell 2.0 中是否有与此“类型”命令等效的命令?
【问题讨论】:
标签: windows shell powershell powershell-2.0 command-line-interface
等价物是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
【讨论】:
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[]}}
【讨论】:
由于您使用 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。
【讨论】:
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 属性中获取该信息。 (与 bash 中的 type 相同,它会告诉您 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 :
CommandType 即Application,gcm <module_name> | select * 将为您提供路径
▶ 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
...
【讨论】: