Run a program by name from PowerShell (similarly to the run box)

前端 未结 2 1534
你的背包
你的背包 2021-01-07 09:45

In Windows we access the Run box with Windows Key + R. Then we can run a program by name (e.g. firefox, snippingtool). Is there a way to run a

2条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-07 10:11

    As @briantist mentioned in his answer (+1): the "Run" dialog (which probably calls the ShellExecuteEx function) checks the subkeys of the App Paths registry key in addition to the paths in the $env:PATH environment variable.

    For emulating the behavior of the "Run" dialog I would, however, add the folders to $env:PATH instead of creating aliases for each executable:

    $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 ';')
    

    By adding this to a PowerShell profile (e.g. your personal profile %UserProfile%\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1), the variable $env:PATH of each PowerShell instance you start is updated with the additional paths from the registry. Note, however, that by adding too many folders you might be hitting length restrictions.

提交回复
热议问题