在最終使用者的 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 有時會安裝在 AppData 或其他位置,而不是程式檔案。

我的問題是,有沒有一種方法可以動態確定 Chrome 在 Windows 電腦上的安裝位置,以便我可以將其附加到 Shortcut.lnk 的「目標」?

答案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

要在批次檔中使用,您需要將百分比加倍,如下所示:

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 有時在兩個地方時,我需要在環境中找到 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 = ""
    }
}

相關內容