Skip to content

Instantly share code, notes, and snippets.

@Razzile
Created June 26, 2019 15:44
Show Gist options
  • Save Razzile/8ddd96b4b6a3a22499a245f7f1ec11bd to your computer and use it in GitHub Desktop.
Save Razzile/8ddd96b4b6a3a22499a245f7f1ec11bd to your computer and use it in GitHub Desktop.
function Launch-App {
param(
[parameter(mandatory = $true)][ValidateNotNullOrEmpty()][string[]]$appname,
[switch] $admin
)
$apps = (New-Object -Com Shell.Application).NameSpace('shell:::{4234d49b-0245-4df3-b780-3893943456e1}').Items()
if ($apps) {
$count = 0
foreach ($app in $apps) {
if ($app.Name -like $appname) {
$count++
if ($admin) {
#"Run as administrator" action
$action = $app.Verbs() | ? { $_.Name.replace('&', '') -eq "Run as administrator" }
}
else {
# "Open" action
$action = $app.Verbs() | ? { $_.Name.replace('&', '') -eq "Open" }
}
if ($action) {
$action.DoIt()
}
else {
Write-Error "Application '$appname' doesn't support this action."
}
}
}
if ($count -eq 0) {
Write-Error "Application '$appname' Not Found."
}
}
else {
Write-Error "An unexpected error has occurred."
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment