在Windows中,我们使用Windows Key + R
访问运行框。然后,我们可以按名称运行程序(例如firefox,snippingtool)。有没有一种方法可以从PowerShell中按名称运行程序?
> run firefox
> run snippingtool
我已经尝试过
Start-Process firefox
,但是它不起作用。编辑
奇怪的是,在PowerShell和“运行”框中,仅键入
notepad
即可打开记事本。但是,键入firefox
或snippingtool
仅在“运行”框中有效,而在PowerShell中无效。
最佳答案
正如@briantist在他的答案(+1)中提到的:“运行”对话框(probably calls the ShellExecuteEx
function)除了检查App Paths
环境变量中的路径之外,还检查$env:PATH
注册表项的子项。
但是,为了模拟“运行”对话框的行为,我将文件夹添加到$env:PATH
中,而不是为每个可执行文件创建别名:
$regkey = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths'
$appPaths = Get-ChildItem $regkey |
Get-ItemProperty |
? { $_.'(default)' } |
select -Expand '(default)' |
Split-Path -Parent |
% { [Environment]::ExpandEnvironmentVariables($_.TrimStart('"')) } |
select -Unique
$env:PATH += ';' + ($appPaths -join ';')
通过将其添加到PowerShell profile(例如,您的个人资料
%UserProfile%\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
)中,您启动的每个PowerShell实例的变量$env:PATH
都会使用注册表中的其他路径进行更新。但是请注意,通过添加太多文件夹,您可能会命中length restrictions。
关于powershell - 通过PowerShell从名称运行程序(类似于运行框),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26414209/