エンドユーザーの Windows マシンで Chrome を動的に検索する

エンドユーザーの Windows マシンで Chrome を動的に検索する

そこで、考えられる限りのあらゆる場所を検索してみましたが、わかりません。答えが非常に簡単なものであることを願っています。状況は次のとおりです。

エンドユーザー用のショートカット リンクを作成しています。これを「shortcut.lnk」と呼びます。エンドユーザーには Chrome がインストールされており、「myFolder」がデスクトップにあると想定できます。重要なのは、このアプリをユーザーの既定のブラウザーではなく、Chrome で開く必要があることです。現在、shortcut.lnk の「ターゲット」として次のものを設定しています。

%ProgramFiles(x86)%\Google\Chrome\Application\chrome.exe --app=%USERPROFILE%\Desktop\myFolder\path\to\app.html

これは、私がテストした 3 台のマシンで動作します。ただし、調査の結果、Chrome が Program Files ではなく AppData などの場所にインストールされる場合があることがわかりました。

私の質問は、shortcut.lnk の「ターゲット」に Chrome を添付できるように、Windows マシン上の Chrome がインストールされている場所を動的に判断する方法があるかどうかです。

答え1

Chrome がインストールされている場所を動的に判断する方法はありますか?

次のコマンドは、Chrome がインストールされている場所を特定し、CHROMEPATH環境変数をこの値に設定します。

for /f "usebackq tokens=1,2,3,4,5" %a in (`reg query HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\ /s /f \chrome.exe ^| findstr Application`) do set CHROMEPATH=%c%d%e

出力例:

echo %CHROMEPATH%
C:\ProgramFiles(x86)\Google\Chrome\Application\chrome.exe

バッチ ファイルで使用するには、次のようにパーセントを 2 倍にする必要があります。

for /f "usebackq tokens=1,2,3,4,5" %%a in (`reg query HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\ /s /f \chrome.exe ^| findstr Application`) do set CHROMEPATH=%%c%%d%%e

答え2

私も同じ問題に遭遇しましたが、@DavidPostill が作成したスクリプトは PowerShell では動作しなかったため、彼の回答に基づいて独自のスクリプトを作成しました。

function Find-PSFilePathInRegistry {
    param (
        [string]$FileName
    )
    $str = reg query HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\ /s /f \chrome.exe | findstr Default
    # regex to find the drive letter until $FileName
    if ($str -match "[A-Z]\:.+$FileName") {
        return @{
            success = $true
            path = $Matches[0]
        }
    }
    else {
        return @{
            success = $false
            path = ""
        }
    }
}

以下のように使用します。

$res = Find-FilePathInRegistry "chrome.exe"
$res.success
True
$res.path
C:\Program Files\Google\Chrome\Application\chrome.exe

見つからない場合は、$res.successになります$false

答え3

そこで、エンドポイントに新しいショートカットを作成するために、アプリケーションを見つけるためにこの機能が必要になりました。この機能は、Chrome が 2 か所にある環境で Chrome 実行ファイルを見つける必要があるため、非常に多くの時間を節約し、作業がずっと楽になりました。ありがとうございます。少し調整したので、共有したいと思います。

<#
.SYNOPSIS
Searches the Windows registry for the path of a specified application file.

.DESCRIPTION
The Find-PSFilePathInRegistry function searches the Windows registry for the path of a specified application file. It allows you to locate the installation directory of an application by searching through the registry keys associated with installed software.

.PARAMETER FileName
Specifies the name of the application file to search for in the registry.

.OUTPUTS
Outputs a custom object with the following properties:
- Success: Indicates whether the search was successful (true/false).
- Path: The full path to the specified application file.
- AppDir: The directory where the application file is located.

.EXAMPLE
Find-PSFilePathInRegistry -FileName "chrome.exe"
Searches the Windows registry for the path of the Google Chrome executable file.

.EXAMPLE
Find-PSFilePathInRegistry -FileName "notepad.exe"
Searches the Windows registry for the path of the Notepad executable file.

.NOTES
The function searches the following registry keys:
- HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths
- HKCU:\Software\Microsoft\Windows\CurrentVersion\App Paths
#>
function Find-PSFilePathInRegistry {
    param (
        [string]$FileName
    )

    # Define an array of common registry locations where application paths are stored
    $registryKeys = @(
        "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths",
        "HKCU:\Software\Microsoft\Windows\CurrentVersion\App Paths"
    )

    # Iterate through each registry key
    foreach ($key in $registryKeys) {
        if (Test-Path $key) {
            # Get the default value (which usually contains the path) for the specified file name
            $value = Get-ItemProperty -Path "$key\$FileName" -Name "(default)" -ErrorAction SilentlyContinue
            if ($value) {
                $appDir = Split-Path -Path $value.'(default)' -Parent
                return @{
                    Success = $true
                    Path = $value.'(default)'
                    AppDir = $appDir
                }
            }
        }
    }

    # If the path is not found in any of the registry keys, return failure
    return @{
        Success = $false
        Path = ""
        AppDir = ""
    }
}

関連情報