PoshCode Archive  Artifact [af3443e475]

Artifact af3443e475eff1409194f56999b6ff39ba6deef77937591dc7c353dbef356279:

  • File Get-App.ps1 — part of check-in [2c5e598810] at 2018-06-10 13:00:15 on branch trunk — Attempt to resolve the path to an executable using Get-Command or the “App Paths” registry key — returns an ApplicationInfo object (user: Joel Bennett size: 1712)

# encoding: ascii
# api: powershell
# title: Get-App
# description: Attempt to resolve the path to an executable using Get-Command or the “App Paths” registry key — returns an ApplicationInfo object
# version: 1.0
# type: function
# author: Joel Bennett
# license: CC0
# function: Get-App
# x-poshcode-id: 170
# x-archived: 2017-05-17T23:43:49
# x-published: 2008-04-11T18:41:00
#
#

## Get-App
## Attempt to resolve the path to an executable using Get-Command and the AppPaths registry key
##################################################################################################
## Example Usage:
##    Get-App Notepad
##       Finds notepad.exe using Get-Command
##    Get-App pbrush
##       Finds mspaint.exe using the "App Paths" registry key
##    &(Get-App WinWord)
##       Finds, and launches, Word (if it's installed) using the "App Paths" registry key
##################################################################################################
## Revision History
## 1.0 - initial release
##################################################################################################
function Get-App {
   param( [string]$cmd )
   $eap = $ErrorActionPreference
   $ErrorActionPreference = "SilentlyContinue"
   Get-Command $cmd
   if(!$?) {
      $AppPaths = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths"
      if(!(Test-Path $AppPaths\$cmd)) {
         $cmd = [IO.Path]::GetFileNameWithoutExtension($cmd)
         if(!(Test-Path $AppPaths\$cmd)){
            $cmd += ".exe"
         }
      }
      if(Test-Path $AppPaths\$cmd) {
         Get-Command (Get-ItemProperty $AppPaths\$cmd)."(default)"
      }
   }
}