Public/Start-Application.ps1
Function Start-Application { <# .DESCRIPTION Launch most applications using powershell and the name of the executable .Parameter Application Specify all or part of the name for an applications EXE .Parameter Index used to build an index of application executables .Example Launch and Application Start-Application -Application outlook .Example Launch an application use the function alias and without specifying the application parameter app outlook .Example Build the index used for launching applications. Can be run again to update the index when needed Start-Application -index #> [cmdletbinding()] [Alias ('app')] param ( [Parameter(Mandatory = $true, Position = 1, ParameterSetName = 'Application')] [String]$Application, [Parameter (Mandatory = $true, ParameterSetName = 'index')] [Switch]$Index ) #Check that admin privleges are used Get-Elevation #Build an index of Executables for querying when launching applications if ($Index) { $ErrorActionPreference = 'silentlycontinue' $P1 = Get-ChildItem "C:\Program Files\" -include "*.exe" -Recurse | Select-Object FullName, PSChildName $P2 = Get-ChildItem "C:\Program Files (x86)\" -include "*.exe" -Recurse | Select-Object FullName, PSChildName $P3 = Get-ChildItem "C:\programdata\chocolatey" -include "*.exe" -Recurse | Select-Object FullName, PSChildName $P4 = Get-ChildItem "C:\Windows\System32" -include "*.exe" -Recurse | Select-Object FullName, PSChildName $P5 = Get-ChildItem "C:\Windows\Syswow64" -include "*.exe" -Recurse | Select-Object FullName, PSChildName $P6 = Get-ChildItem "$env:USERPROFILE\appdata" -include "*.exe" -Recurse | Select-Object FullName, PSChildName $ErrorActionPreference = 'continue' $AppIndex = $P1 + $P2 + $P3 + $P4 + $P5 + $P6 $AppIndex | Export-Clixml $env:USERPROFILE\ApplicationIndex.xml } #Launch an application else { #Check that the index exists, Import it, and execute a specific function based off how many executables are returned if (Test-Path $env:USERPROFILE\ApplicationIndex.xml) { $IndexImport = Import-Clixml $env:USERPROFILE\ApplicationIndex.xml $Program = $IndexImport | Where-Object { $_.PSChildName -like "*$Application*.exe" } if ($Program.FullName.count -eq '0' ) { Write-Warning "Program Not Found! Try changing your application parameter or rebuild the index using the index parameter" } elseif ($Program.FullName.count -gt '1' ) { Write-Host " " Write-Warning "More than one application found! Next time try reducing the number of found applications to 1, by being more specific with the Application Name." Write-Host " " Write-Host "You can use the gridview to open the application you desire." -ForegroundColor Green $Selection = $Program.FullName | Out-GridView -PassThru -Title "Select Your desired Applicaiton to Open" Start-Process $Selection } else { Start-Process $Program.FullName } } #If the index doesn't exist, warn to build it. else { Write-Host Write-Warning "No index file found. First build the index using: Start-Applicaiton -index" } } } |