<# 移除 MoonChromeStore 的環境設定: - ExtensionInstallSources 裡的 Store 網址 - ExtensionInstallAllowlist 裡由 MoonChromeStore 加入的 ID(記錄在 HKLM\SOFTWARE\MoonChromeStore) 其他既有設定保留並重新編號。移除後,從 Store 安裝的擴充功能會被 Chrome 停用。 用法:powershell -ExecutionPolicy Bypass -File remove-policy.ps1 [-Origin http://localhost:8787] #> param( [string]$Origin = '' ) $ErrorActionPreference = 'Stop' $isAdmin = ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole( [Security.Principal.WindowsBuiltInRole]::Administrator) if (-not $isAdmin) { Write-Host '請以「系統管理員身分」開啟 PowerShell 後再執行這個腳本。' -ForegroundColor Red exit 1 } $trackKey = 'HKLM:\SOFTWARE\MoonChromeStore' $track = Get-ItemProperty $trackKey -ErrorAction SilentlyContinue if (-not $Origin) { $Origin = if ($track.Origin) { $track.Origin } else { 'http://localhost:8787' } } $pattern = "$($Origin.TrimEnd('/'))/*" $managedIds = @($track.ManagedIds | Where-Object { $_ }) # 從清單型政策移除指定的值,剩下的從 1 重新連續編號 function Remove-ListPolicyValues([string]$Key, [string[]]$Values) { if (-not (Test-Path $Key)) { return 0 } $props = @((Get-ItemProperty $Key).PSObject.Properties | Where-Object { $_.Name -match '^\d+$' } | Sort-Object { [int]$_.Name }) $keep = @($props | Where-Object { $Values -notcontains $_.Value } | ForEach-Object { $_.Value }) $removed = $props.Count - $keep.Count if ($removed -eq 0) { return 0 } foreach ($p in $props) { Remove-ItemProperty -Path $Key -Name $p.Name } for ($n = 0; $n -lt $keep.Count; $n++) { New-ItemProperty -Path $Key -Name "$($n + 1)" -Value $keep[$n] -PropertyType String -Force | Out-Null } if ($keep.Count -eq 0) { Remove-Item -Path $Key } return $removed } foreach ($b in @( @{ Name = 'Chrome'; Key = 'HKLM:\SOFTWARE\Policies\Google\Chrome' }, @{ Name = 'Edge'; Key = 'HKLM:\SOFTWARE\Policies\Microsoft\Edge' })) { $s = Remove-ListPolicyValues (Join-Path $b.Key 'ExtensionInstallSources') @($pattern) $a = if ($managedIds.Count) { Remove-ListPolicyValues (Join-Path $b.Key 'ExtensionInstallAllowlist') $managedIds } else { 0 } Write-Host "$($b.Name):移除安裝來源 $s 筆、白名單 $a 筆。" -ForegroundColor Green } if (Test-Path $trackKey) { Remove-Item -Path $trackKey -Recurse } Write-Host '完成。請重新開啟 Chrome / Edge。'