Home > OS >  How to get Paths to all executables from Get-ChildItem
How to get Paths to all executables from Get-ChildItem

Time:04-16

I am currently trying to get a list of all installed applications and would like to build a feature that can launch those.

I'm using these PowerShell commands:

gci HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\* | % { Get-ItemProperty $_.PsPath } | Select DisplayName,InstallLocation

gci HKLM:\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\* | % { Get-ItemProperty $_.PsPath } | Select DisplayName,InstallLocation

in conjunction with ConvertTo-Json in order to get a good stdout I can work with. Now, this only gives me the InstallPath without any executables.

Is there any easy way to get the main executable of the applications i nthe list?

Expected Result (Name of the key does not matter):

    // ...
    {
        "DisplayName":  "Microsoft Edge",
        "InstallLocation":  "C:\\Program Files (x86)\\Microsoft\\Edge\\Application",
        "LaunchApplication": "C:\\Program Files (x86)\\Microsoft\\Edge\\Application\msedge.exe",
    },
    {
        "DisplayName":  "Audacity 2.4.2",
        "InstallLocation":  "C:\\Program Files (x86)\\Audacity\\",
        "LaunchApplication": "C:\\Program Files (x86)\\Audacity\\audacity.exe"
    },
    // ...

CodePudding user response:

Like others have pointed out in the comments, there isn't a conventional way of getting the executable paths of certain programs.

To answer your indirect question of building an app launch method, we can make use of a few things. Fortunately for us, PowerShell has a Get-StartApps cmdlet that produces an output of the current users installed apps:


Name                                                 AppID
----                                                 -----
3D Viewer                                            Microsoft.Microsoft3DViewer_8wekyb3d8bbwe!Microsoft.Microsoft3DViewer
AdGuard                                              AdGuard
Adobe Acrobat DC                                     {6D809377-6AF0-444B-8957-A3773F02200E}\Adobe\Acrobat DC\Acrobat\Acrobat.exe
Battle.net                                           {7C5A40EF-A0FB-4BFC-874A-C0F2E0B9FA8E}\Battle.net\Battle.net Launcher.exe
Blend for Visual Studio 2022                         Blend.d58ce8bb
Calculator                                           Microsoft.WindowsCalculator_8wekyb3d8bbwe!App
Calendar                                             microsoft.windowscommunicationsapps_8wekyb3d8bbwe!microsoft.windowslive.calendar

There are 2 properties that are displayed:

  • Name
  • AppID.

This becomes important due to the AppID being the value needed for shell: to execute/launch the program. Given the above output of Get-StartApps, you can launch "Adobe Acrobat DC" by passing the AppID to shell:\AppsFolder\"AppID".

Start-Process shell:AppsFolder\"{6D809377-6AF0-444B-8957-A3773F02200E}\Adobe\Acrobat DC\Acrobat\Acrobat.exe"

Using @zett42's approach, we can query your start menu, along with the system start menu folder paths for .lnk's retrieving its target path using the WScript COM object:

$paths = "C:\ProgramData\Microsoft\Windows\Start Menu\Programs","$env:APPDATA\Microsoft\Windows\Start Menu"
Get-ChildItem -Path $paths -Filter "*.lnk" -File -Recurse | 
    ForEach-Object -Begin {
        $WScriptShell = New-Object -ComObject "WScript.Shell"
    } -Process {
        [PSCustomObject]@{
            Name = $_.BaseName
            Path = $WScriptShell.CreateShortcut($_.FullName).TargetPath
        }
    } -End {
        [void][System.Runtime.Interopservices.Marshal]::ReleaseComObject($WScriptShell) #release COM object
    }

which will output:

Name                         Path
----                         ----
Adobe Acrobat DC             C:\Program Files\Adobe\Acrobat DC\Acrobat\Acrobat.exe
Blend for Visual Studio 2022 C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\Blend.exe
Firefox                      C:\Program Files\Mozilla Firefox\firefox.exe
Google Chrome                C:\Program Files\Google\Chrome\Application\chrome.exe
Microsoft Edge               C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe
TechPowerUp GPU-Z            C:\Program Files (x86)\GPU-Z\GPU-Z.exe

Not entirely sure this is what you're after, but it may be of help to others.

  • Related