在许多情况下,我需要一个指向可执行文件或命令行工具的路径,例如:notepad
或kubectl
。虽然使用PowerShell,但可执行文件是可用的,但文件的物理位置并不总是很容易找到。
一种方法是搜索路径上的每个文件夹,或者更糟的是系统( gci -r | % { $_ ...}
),但这不是最有效的时间利用方式,每次都要重新编码。有没有更好的方法?
发布于 2021-06-25 22:03:05
Get-Command
将返回一个对象,其中包含几个带有路径名的字段。例如,如果我在我的系统上输入Get-Command notepad
,我会得到
PS Z:\> Get-Command notepad
CommandType Name Version Source
----------- ---- ------- ------
Application notepad.exe 10.0.18... C:\WINDOWS\system32\notepad.exe
如果我输入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 :
发布于 2021-06-25 21:49:40
我最近发现PowerShell可以使用Where.exe
。
> Where.exe kubectl
C:\Program Files\Docker\Docker\resources\bin\kubectl.exe
这与PowerShell变量兼容,因此可以在编写脚本时使用:$path = where.exe notepad
https://stackoverflow.com/questions/68132101
复制相似问题