Skip to content

Instantly share code, notes, and snippets.

@vdepagter
Last active March 10, 2023 14:52
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vdepagter/c3a66526467c381bc0b416ca879183c8 to your computer and use it in GitHub Desktop.
Save vdepagter/c3a66526467c381bc0b416ca879183c8 to your computer and use it in GitHub Desktop.
Configure chrome://flags (enabled_labs_experiments) through users' Local State file
# Source: https://gist.github.com/vdepagter/c3a66526467c381bc0b416ca879183c8
# Requires all Chrome processes to be stopped (check persistent task bar).
# Modify $SetProperties as desired. These flags will be added or modified. Existing flags are preserved.
If (Test-Path "I:\Chrome") {
Start-Transcript "I:\Chrome\Set-ChromePrefs.log"
[array]$SetProperties = 'enable-native-notifications@2','enable-system-notifications@2'
try {
If (Test-Path "HKCU:\Software\Policies\Google\Chrome") { $LocalStateFile = (Get-ItemProperty "HKCU:\Software\Policies\Google\Chrome").UserDataDir + '\Local State' }
If (-not $LocalStateFile) { $LocalStateFile = "$($env:localappdata)\Google\Chrome\User Data\Local State" }
#notepad $LocalStateFile
If (Test-Path $LocalStateFile) {
$LocalState = (Get-Content -LiteralPath $LocalStateFile) -Replace [regex]::escape('"":'),'"_emptyName":' # ConvertFrom-Json issue: https://github.com/PowerShell/PowerShell/issues/1755
$JsonContent = ConvertFrom-Json -InputObject $LocalState
$JsonContent.browser | fl enabled_labs_experiments
# If browser property does not exist, add it
If (-not $JsonContent.browser) { $JsonContent | Add-Member -Type NoteProperty -Name 'browser' -Value $null }
# If browser.enabled_labs_experiments property does not exist, add it
If (-not (($JsonContent.browser | gm) |?{$_.Name -eq 'enabled_labs_experiments'})) { $JsonContent.browser | Add-Member -Type NoteProperty -Name 'enabled_labs_experiments' -Value ([array]$SetProperties) }
foreach ($property in $SetProperties) {
# If browser.enabled_labs_experiments already contains the flag, remove it first
If ($JsonContent.browser.enabled_labs_experiments -match ($property -Replace '@.*')) {
Write-Host -b black -f yellow "Removing already existing flag $($property -Replace '@.*')"
[array]$JsonContent.browser.enabled_labs_experiments = $JsonContent.browser.enabled_labs_experiments | ?{$_ -notmatch ($property -Replace '@.*')}
}
# Add flag
Write-Host -b black -f green "Adding flag $property"
$JsonContent.browser.enabled_labs_experiments += [array]$property
}
$JsonContent.browser | fl enabled_labs_experiments
$NewLocalState = (ConvertTo-Json -Depth 32 -InputObject $JsonContent -Compress) -Replace [regex]::escape('"_emptyName":'),'"":'
} Else {
md -EA Si (($LocalStateFile -Split '\\' | Select -SkipLast 1) -Join '\') # Create directory if necessary
$NewLocalState = '{"browser":{"enabled_labs_experiments":["enable-system-notifications@2","enable-native-notifications@2"]}}'
}
Set-Content -Value $NewLocalState -LiteralPath $LocalStateFile
} catch { Write-Host -b black -f red "Error occurred: $($Error[0])"}
Stop-Transcript
} Else {
Write-Host -b black -f red "ERROR: `"I:\Chrome`" directory not found"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment