r/PowerShell • • 1d ago

Solved Script to resolve missing MSI Cache for google chrome (1603,1612)

Just sharing this because ive had this issue across 1500 devices and with the help of copilot this script has been generated and has been resolving the issues perfectly.

Due to missing MSI Caches, Upgrades for chrome are failing with MSI 1603 or MSI 1612. This script pulls the latest, forcefully removes the old chrome, and installs fresh

 # ==========================
# Google Chrome Remediation
# ==========================

$DownloadUrl = "https://dl.google.com/dl/chrome/install/googlechromestandaloneenterprise64.msi"
$TempDir     = "C:\ProgramData\ChromeRemediation"
$MSIPath     = Join-Path $TempDir "GoogleChromeStandaloneEnterprise64.msi"

Write-Host "Starting Chrome remediation..."

# Ensure TLS 1.2+ is enabled for older PowerShell / Windows builds
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 -bor [Net.SecurityProtocolType]::Tls13

# --------------------------------------------------
# Download Latest Chrome Enterprise 64-bit MSI
# --------------------------------------------------

New-Item -ItemType Directory -Path $TempDir -Force | Out-Null

Write-Host "Downloading latest Chrome Enterprise 64-bit MSI..."

try {
    # Suppress progress bar to prevent severe Invoke-WebRequest slowdowns in PowerShell 5.1
    $ProgressPreference = 'SilentlyContinue'
    Invoke-WebRequest -Uri $DownloadUrl -OutFile $MSIPath -UseBasicParsing -ErrorAction Stop
}
catch {
    Write-Error "Failed to download Chrome MSI: $_"
    exit 1
}

# Verify the file exists and is valid (at least 50 MB to rule out HTML error pages)
if (-not (Test-Path $MSIPath) -or (Get-Item $MSIPath).Length -lt 50MB) {
    Write-Error "Downloaded MSI is missing or smaller than expected. Aborting remediation."
    exit 1
}

Write-Host "Download verified ($([math]::Round((Get-Item $MSIPath).Length / 1MB, 2)) MB)."

# --------------------------------------------------
# Backup bookmarks
# --------------------------------------------------

$BackupRoot = "C:\ProgramData\ChromeBookmarkBackup"

New-Item -ItemType Directory -Path $BackupRoot -Force | Out-Null

Get-ChildItem "C:\Users" -Directory -ErrorAction SilentlyContinue | ForEach-Object {

    $User = $_.Name

    Get-ChildItem "$($_.FullName)\AppData\Local\Google\Chrome\User Data" `
        -Directory `
        -ErrorAction SilentlyContinue |

    ForEach-Object {

        $BookmarkFile = Join-Path $_.FullName "Bookmarks"

        if (Test-Path $BookmarkFile) {

            $BackupName = "$User-$($_.Name)-Bookmarks.json"

            Copy-Item `
                $BookmarkFile `
                (Join-Path $BackupRoot $BackupName) `
                -Force `
                -ErrorAction SilentlyContinue

            Write-Host "Backed up bookmarks for $User ($($_.Name))"
        }
    }
}

# --------------------------------------------------
# Kill Chrome Processes
# --------------------------------------------------

Get-Process chrome,googleupdate,googleupdater `
    -ErrorAction SilentlyContinue |
    Stop-Process -Force

# --------------------------------------------------
# Remove Chrome Uninstall Keys
# --------------------------------------------------

$UninstallRoots = @(
    "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall",
    "HKLM:\Software\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall"
)

foreach ($Root in $UninstallRoots) {

    Get-ChildItem $Root -ErrorAction SilentlyContinue | ForEach-Object {

        $App = Get-ItemProperty $_.PSPath -ErrorAction SilentlyContinue

        if ($App.DisplayName -eq "Google Chrome") {

            Write-Host "Removing uninstall key $($_.PSChildName)"

            Remove-Item $_.PSPath `
                -Recurse `
                -Force `
                -ErrorAction SilentlyContinue
        }
    }
}

# --------------------------------------------------
# Remove Windows Installer Product Registrations
# --------------------------------------------------

Get-ChildItem `
    "Registry::HKEY_CLASSES_ROOT\Installer\Products" `
    -ErrorAction SilentlyContinue |

ForEach-Object {

    $Item = Get-ItemProperty $_.PSPath -ErrorAction SilentlyContinue

    if ($Item.ProductName -eq "Google Chrome") {

        Write-Host "Removing installer registration $($_.PSChildName)"

        Remove-Item $_.PSPath `
            -Recurse `
            -Force `
            -ErrorAction SilentlyContinue
    }
}

# --------------------------------------------------
# Remove Program Files Installs
# --------------------------------------------------

Remove-Item `
    "C:\Program Files\Google\Chrome" `
    -Recurse `
    -Force `
    -ErrorAction SilentlyContinue

Remove-Item `
    "C:\Program Files (x86)\Google\Chrome" `
    -Recurse `
    -Force `
    -ErrorAction SilentlyContinue

# --------------------------------------------------
# Install Latest Chrome
# --------------------------------------------------

$Process = Start-Process `
    msiexec.exe `
    -ArgumentList "/i `"$MSIPath`" /qn /norestart" `
    -Wait `
    -PassThru

Write-Host "Chrome MSI exit code: $($Process.ExitCode)"

# Clean up the downloaded installer
Remove-Item $TempDir -Recurse -Force -ErrorAction SilentlyContinue

if ($Process.ExitCode -in @(0,3010)) {

    Write-Host "Chrome installation successful"

} else {

    Write-Host "Chrome installation failed"
    exit $Process.ExitCode
}

# --------------------------------------------------
# Validation
# --------------------------------------------------

$Chrome = Get-ItemProperty `
HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*, `
HKLM:\Software\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* `
-ErrorAction SilentlyContinue |
Where-Object {$_.DisplayName -eq "Google Chrome"}

if ($Chrome) {

    Write-Host "Installed Version: $($Chrome.DisplayVersion)"

} else {

    Write-Host "Chrome not detected after install"
    exit 1

}

15 Upvotes

8 comments sorted by

3

u/BlackV 1d ago

Have a think about not using back ticks like that

https://get-powershellblog.blogspot.com/2017/07/bye-bye-backtick-natural-line.html

are you running this in intune (or similar)?

1

u/JustAnITGuyAtWork11 1d ago

We are deploying this using Action1 Patch managment. Our old patch management tool totally butchered the MSI files (see my comment reply to /u/Advanced_Day8657 )

After fighting with this manually by removing with Revo and performing manual reinstallation i turned to copilot to create me a script.

Figured id share as i hadnt seen anything like this before online and has saved our arses. Ive made (Well, Copilot has) a few others for some other tools too which have evergreen "latest" download links that also have broken MSI files.

Thank you for the feedback RE the back tics - will take that on board

1

u/BlackV 1d ago

Ah nice Action 1

I'm looking at putting the enterprise catalogue version of chrome out there via intune and some cleanup remediations

3

u/Fallingdamage 1d ago

AI likes backticks.

3

u/BlackV 1d ago

It really does

My favorite line is

yes you explicitly told me not to use back ticks and I still used back ticks, my mistake, here is the updated code with out back ticks

    get-disk `
        -number 2

FFFFFFFFFUUUUUUUUUUU

1

u/Advanced_Day8657 1d ago

Very cool. Chrome was unable to auto update?

1

u/JustAnITGuyAtWork11 1d ago

Thats correct.

One of our old patch management tools totally butchered the MSI files when doing cleanups of its install caches. Long story short instead of placing the MSI cache under C:\windows\installer, it placed them under their own directory, then when the agent performs clean ups when low disk space is flagged, it removed them meaning the applications couldnt find the msi file to remove or update.

This script has been running on a number of machines and gives an output like this:

PS C:\WINDOWS\System32> Invoke-ChromeRemediation Starting Chrome remediation... Downloading latest Chrome Enterprise 64-bit MSI... Download verified (167.75 MB). Backed up bookmarks for USER1 (Default) Backed up bookmarks for USER2 (Default) Backed up bookmarks for USER3 (Default) Backed up bookmarks for USER4 (Default) Backed up bookmarks for USER5 (Default) Removing installer registration D6B8E4B1713836033BC96DFE6EA2D674 Chrome MSI exit code: 0 Chrome installation successful Installed Version: 158.0.8127.37 PS C:\WINDOWS\System32>

1

u/gadget850 1d ago

I get issues like this where the install is in c:\windows\ccmcache and got auto cleared. The MicrosoftProgram_Install_and_Uninstall.meta.diagcab still works to clean out the registry for these issues since our Enterprise installs have the troubleshooters disabled. I have it as a Bomgar script.