Adding a new homepage in Chrome preference file (Powershell)

Durgesh007 1 Reputation point

We would like to replace old homepage and add a new homepage in Chrome Preference file using Powershell. My code does below things

  1. read the pref file and save all the existing homepage in a new var
  2. if found old homepage, remove that and add new homepage

But now, i am unable to save the new var in the pref file. The file is in json format.

$ChromePrefFile = "$($env:LOCALAPPDATA)\Google\Chrome\User Data\Default\Preferences"
$Settings = Get-Content $ChromePrefFile | ConvertFrom-Json

$homepage = $Settings.session.startup_urls

$UrlOutPut =  ""
foreach ($hg in $homepage) 
if ($hg -ne $oldhp )
    $UrlOutPut = $UrlOutPut  + "'$hg',"

$UrlOutPut = "'$newhp'," + $UrlOutPut
Write-Host $UrlOutPut

How to write the new value in pref file?

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,435 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Ian Xue (Shanghai Wicresoft Co., Ltd.) 32,326 Reputation points Microsoft Vendor


    If $UrlOutPut is the value of startup_urls you can save it like below

    $Settings.session.startup_urls = $UrlOutPut  
    $Settings | ConvertTo-Json | Out-File $ChromePrefFile  

    Best Regards,
    Ian Xue


    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

  2. Joshua Heinreich 1 Reputation point

    I've been playing with this all afternoon, somethings I've discovered:

    1. 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)
    2. 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
    3. 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 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 = "",""
        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
    0 comments No comments

  3. el_chema 1 Reputation point

    hi. error "restore_on_startup": 1,

    0 comments No comments