I've been playing with this all afternoon, somethings I've discovered:
- If the Chrome profile is defaulted (due to a corrupt preference file) it's missing the session block, so it needs to be added in and can't be edited (cause it doesn't exist)
- If the user is using profiles, the default preference file is deleted and "profile #" folders popup in it's place, # being sequenced starting at 1
- I couldn't figure out how to insert arrays into a JSON block, but I do know how to change an existing setting to a powershell array's value.
That all said, here's some sloppy code to deal with those conditions. Bear in mind it will only edit "Profile 1" if "Default" doesn't exist and it will not edit any other profiles (sorry Profile 2).
EDIT: I found out the hard way that -ConvertTo-Json by default only writes settings to 2 steps deep. That behavior was omitting a bunch of settings in the preferences file. Chrome handled it alright, but would occasionally crash with photos.google.com. Edge didn't like it at all and would kill itself shortly after loading. I added in the -depth switch and feel safe with writing it up to 99 steps deep. I also added in some parameters to show the home button and default it over to the first homepage defined in the array.
Microsoft Edge is a Chromium variant, so this should work on Edge as well if the profile path is changed to
$($env:LOCALAPPDATA)\Local\Microsoft\Edge\User Data
if(test-path "$($env:LOCALAPPDATA)\Google\Chrome\User Data\Default\Preferences") {
$ChromePrefFile = "$($env:LOCALAPPDATA)\Google\Chrome\User Data\Default\Preferences"
} elseif (test-path "$($env:LOCALAPPDATA)\Google\Chrome\User Data\Profile 1\Preferences") {
$ChromePrefFile = "$($env:LOCALAPPDATA)\Google\Chrome\User Data\Profile 1\Preferences"
}
$Settings = Get-Content $ChromePrefFile -Encoding UTF8 | ConvertFrom-Json
$newhp = "https://www.tactical-tech.net","https://www.google.com"
if(! $Settings.session.startup_urls) {
$sBlock = @"
{
"restore_on_startup": 1,
"startup_urls": [
""
]
}
"@
#Remove all leading blank spaces in front of the QUOTE AT characters on the line above
$Settings | Add-Member -name "session" -Value (convertfrom-json $sBlock) -MemberType NoteProperty
}
if(! $Settings.homepage_is_newtabpage) {
$Settings | Add-Member -name "homepage_is_newtabpage" -value "" -MemberType NoteProperty -force
}
if(! $settings.browser.show_home_button) {
$Settings.browser | Add-Member -name "show_home_button" -value "" -MemberType NoteProperty -force
}
if(! $settings.homepage) {
$Settings | Add-Member -name "homepage" -value "" -MemberType NoteProperty -force
}
$Settings.session.startup_urls = @($newhp)
$settings.session.restore_on_startup = 4
$Settings.homepage = $newhp[0]
$Settings.homepage_is_newtabpage = $false
$settings.browser.show_home_button = $true
$Settings | ConvertTo-Json -compress -Depth 99 | Out-File $ChromePrefFile -Encoding UTF8