[英]From PowerShell, how can the location of an executable be resolved?
On many occasions I have needed a path to an executable or command line tool, for example: notepad
or kubectl
.在很多情况下,我需要一个可执行文件或命令行工具的路径,例如: notepad
或kubectl
。 Whilst using PowerShell, the executable is available, but the physical location of the file has not always been easy to find.在使用 PowerShell 时,可执行文件可用,但文件的物理位置并不总是很容易找到。
One way is to search every folder on the PATH, or even worse the system ( gci -r | % { $_ ...}
), but this is not the most efficient use of time, to re-code every time.一种方法是搜索 PATH 上的每个文件夹,甚至更糟的系统( gci -r | % { $_ ...}
),但这不是最有效地利用时间,每次都重新编码。 Is there a better way?有没有更好的办法?
Get-Command
will return an object that contains several fields with the pathname. Get-Command
将返回一个包含多个带有路径名的字段的对象。 For example, if I type Get-Command notepad
on my system, I get例如,如果我在系统上键入Get-Command notepad
,我会得到
PS Z:\> Get-Command notepad
CommandType Name Version Source
----------- ---- ------- ------
Application notepad.exe 10.0.18... C:\WINDOWS\system32\notepad.exe
and if I type Get-Command notepad | Select-Object *
如果我输入Get-Command notepad | Select-Object *
Get-Command notepad | Select-Object *
I will get Get-Command notepad | Select-Object *
我会得到
PS Z:\> Get-Command notepad | Select-Object *
HelpUri :
FileVersionInfo : File: C:\WINDOWS\system32\notepad.exe
InternalName: Notepad
OriginalFilename: NOTEPAD.EXE.MUI
FileVersion: 10.0.18362.1 (WinBuild.160101.0800)
FileDescription: Notepad
Product: Microsoft® Windows® Operating System
ProductVersion: 10.0.18362.1
Debug: False
Patched: False
PreRelease: False
PrivateBuild: False
SpecialBuild: False
Language: English (United States)
Path : C:\WINDOWS\system32\notepad.exe
Extension : .exe
Definition : C:\WINDOWS\system32\notepad.exe
Source : C:\WINDOWS\system32\notepad.exe
Version : 10.0.18362.1316
Visibility : Public
OutputType : {System.String}
Name : notepad.exe
CommandType : Application
ModuleName :
Module :
RemotingCapability : PowerShell
Parameters :
ParameterSets :
I recently discovered Where.exe
is available to PowerShell.我最近发现Where.exe
可用于 PowerShell。
> Where.exe kubectl
C:\Program Files\Docker\Docker\resources\bin\kubectl.exe
This is compatible with PowerShell variables so can be used whilst scripting: $path = where.exe notepad
这与 PowerShell 变量兼容,因此可以在编写脚本时使用: $path = where.exe notepad
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.