powershell - 通过PowerShell从名称运行程序(类似于运行框)

标签 powershell

在Windows中,我们使用Windows Key + R访问运行框。然后,我们可以按名称运行程序(例如firefox,snippingtool)。有没有一种方法可以从PowerShell中按名称运行程序?

> run firefox
> run snippingtool

我已经尝试过Start-Process firefox,但是它不起作用。

编辑

奇怪的是,在PowerShell和“运行”框中,仅键入notepad即可打开记事本。但是,键入firefoxsnippingtool仅在“运行”框中有效,而在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/

相关文章:

.net - 如何将ManagementObjectCollection对象转换为Powershell可读的格式?

powershell 打印到另一个用户的默认打印机

powershell - Powershell函数返回字符串,但客户端获取数组

powershell - 导入 PSSession 在脚本中失败,在 shell 中工作

powershell - 从 Windows 10 powershell 设置 "sideload apps"(更新和安全 > 对于开发人员)

.net - 如何使用 .NET 确定 Windows 上网络接口(interface)的绑定(bind)顺序?

c# - "Enable Always on support..."用于通过 C# 或 powershell 的 SSIS

networking - 如何使用Powershell配置Juniper Networks SA VPN连接设置

powershell - 在Powershell中使用NSSM安装服务

Powershell 删除 powershell 输出中的尾随空格