简体   繁体   English

从 PowerShell 中,如何解析可执行文件的位置?

[英]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 .在很多情况下,我需要一个可执行文件或命令行工具的路径,例如: notepadkubectl 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.

相关问题 如何使用正确解析的参数从 powershell 开始新的 powershell window - How to start new powershell window from powershell with properly resolved parameters 如何使用powershell脚本中的参数调用可执行文件 - How to call an executable with parameters from powershell script 如何使用PowerShell中的凭据从本地复制到远程位置? - How can I copy from local to a remote location with credentials in PowerShell? 如何使用可执行脚本块和 ApartmentState 参数在 PowerShell 中设置线程? - How can I set thread in PowerShell with executable scriptblock and ApartmentState argument? 如何使用 PowerShell 提取所有服务的“可执行文件路径” - How can I extract “Path to executable” of all services with PowerShell 如何从 Powershell 中解析 signtool 位置 - How to resolve the signtool location from Powershell 如何执行从Powershell添加到PATH的可执行文件? - How to execute executable which added to PATH from powershell? 如何从PowerShell将字符串和数组的混合形式传递给可执行文件? - How to pass a mixture of strings and arrays to an executable from PowerShell? 如何从SharePoint 2010工作流中调用可执行文件或Powershell脚本? - How to invoke executable or powershell script from SharePoint 2010 Workflow? 如何创建powershell脚本以将数千个日志文件从一个位置移动到另一个位置? - How can i create a powershell script to move thousands of log files from one location to another?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM