Set the first home page tab while keeping existing tabs in Google Chrome via PowerShell

Getting started:  Example Script (Zip), Example Script (preview)
Related article(s):  Set the first home page tab while keeping existing tabs in Microsoft IE

As a continuation of our post on IE, I’ll outline the steps we go through in the body of the example script provided.  In case you haven’t read through the IE tabs post, let me set the stage for the scenario we will walk through:

Consider a portal project roll out requirement stating the following requirements:

– You need to set the default home page tab for the supported browser(s).

This sounds easy enough.

– In doing so you need to ensure the preferred URL is always the first tab.

Yeah, ok.  That’s doable, and not terribly difficult.

– In doing so you need to keep any existing tabs that the user already has that

Ooohhh now we’re getting tricky.  Still, we’ve tackled that before.

– In doing so you need to know that, we support Chrome as a default browser here but do not have Chrome policy management implemented.

. . . Crickets . . .

Ummm, alright …

Now in the past this could be tackled with registry manipulation for both IE and Chrome.  However, with modern Chrome clients, the default tabs along with MANY other browser settings are tracked in a different location.  Most of the settings for a locally managed client (or rather, not managed by policy) have been relocated to a set of files in Chrome’s associated AppData folder.  The file that we will be manipulating is located here:

“%LOCALAPPDATA%\Google\Chrome\User Data\Default\Preferences”

If you were to open this file you will see that it is a JSON like data structure with lots of bits and bobs from the Chrome settings page all tracked in the file:

image

The method we will use to implement these settings is a PowerShell script which can be incorporated to login script or manually run.  To summarize the process I’ll outline it here and we can follow the steps along as we go:

1.  Start (with URL variable)
2.  Gather existing tabs to array variable for enumeration
3.  Build the variable string to replace with
4.  Replace with New tabs variable in the settings file

Let’s examine the script now.

Step 1: Start the script:
Similarly to our IE script, we need to initialize the script run with the a variable containing the new default home page tab’s URL.  Our example script does that with via a parameter and as you can see here is set with an initial default value:

#'http://www.perpetualreality.com/blog/2019/03/29/set-the-first-ho…-in-microsoft-ie/'
#region Establish Script Parameters
    PARAM(
        [Parameter(Position=0, Mandatory=$false)]
        [System.String]
        $global:newRootURL = 'https://wp.me/pawO3b-1x'
    );
#endregion

If you prefer not to use a parameter in your script, that’s fine too.  You can see that (because the new URL is actually required) the example also provides a way to set the static variable too:

if( !($global:newRootURL) -or ($global:newRootURL -eq '')) { $global:newRootURL = 'http://www.perpetualreality.com/blog' };

Step 2: Gather existing tabs for enumeration:
Next we need to read in the existing tabs so that we are able to iterate through them as we apply them back to the user’s ancillary tabs (after the default home page).

$chromePrefPath = ($env:LOCALAPPDATA + "\Google\Chrome\User Data\Default\")
$cDefaultPrefsPath = ($chromePrefPath + "Preferences");
$cPrefs = Get-Content -Raw -Path $cDefaultPrefsPath | ConvertFrom-Json;

You will notice here that we are collecting the entire file into an enumerable array type using the ConvertFrom-JSON cmdlet.  As mentioned before the Preferences file is stored in a minimized JSON data structure and this cmdlet can parse it for us to easily traverse to the existing tabs entries when running the logic to determine what tabs go where.

Step 3. Build the variable string to replace with
This step was a bit tricky to figure out.  ne would think that since we easily read the Preferences file into PowerShell with a ConvertFrom-JSON cmdlet, we could just as easily manipulate the data and output to the file with a ConvertTo-JSON.

NOPE!

Since we can’t do that, we will revert to old school string replacement.  But first, we need to build both strings, the string we are replacing, and the string we are replacing it with. Here are the primers:

$stringToReplace = '"startup_urls":[';
$replacementString = '"startup_urls":[';

Then iterate through each URL from the Preferences file ‘startup_URLs’ section.

if( $cPrefs.session.startup_urls.count -gt 0 ){
    $global:newSecondariesArr = @($global:newRootURL);
    $cPrefs.session.startup_urls | %{ 
        $secondaryURL = $_;
        addSecondary $secondaryURL $global:newRootURL; 
        $stringToReplace = $stringToReplace + '"' + $secondaryURL + '",'
    }    
} else {
#    Write-Host "No Ancillary URLs found";
}

You may notice here, that while iterating through the collection of URLs, we also call another function ‘addSecondary’.  This function’s purpose is to collect the URLs that may already exist and are not the URL designated for the primary tab.

            function addSecondary($secVal, $newRootVal){
                if( 
                    #Existing string not equal www.perpetualreality.com/
                    ($secVal.ToLower() -ne $newRootVal.ToLower()) -and ($secVal.ToLower() -ne ($newRootVal + '/').ToLower()) -and 

                    #Existing string not equal http://www.perpetualreality.com or http://www.perpetualreality.com/
                    ($secVal.ToLower() -ne ('http://' + $newRootVal).ToLower() ) -and ($secVal.ToLower() -ne ('http://' + $newRootVal + '/').ToLower() ) -and

                    #Existing string not equal https://www.perpetualreality.com or https://www.perpetualreality.com/
                    ($secVal.ToLower() -ne ('https://' + $newRootVal).ToLower() ) -and ($secVal.ToLower() -ne ('https://' + $newRootVal + '/').ToLower() ) -and

                    #Because deep down we are all SharePoint people
                    ($secVal.ToLower() -ne ($newRootVal + 'pages/default.aspx').ToLower() )
                ){
                    $global:newSecondariesArr += ($secVal);
                }
            }

Now that we have the sites added to our string to be replaced, we need to append these collected sites into the string that we are replacing  with.

$global:newSecondariesArr |% {
    $replacementString = $replacementString + '"' + $_ + '",'
}

And then close out the string syntax for both variables..

$stringToReplace = $stringToReplace.TrimEnd(',') + "]";
$replacementString = $replacementString.TrimEnd(',') + "]";

Finally, we do the replacement of the strings in the Preferences file:

(Get-Content (Join-Path $chromePrefPath $renamedPrefsFile) ).replace($stringToReplace, $replacementString) | Set-Content $cDefaultPrefsPath

As mentioned on our corresponding post: Set the first home page tab while keeping existing tabs in Microsoft IE, your use case and mileage may vary.  For the use case outlined above, our recommended best practice for implementation was to set this script to run at every login via GPO (in tandem with the IE logic if also required).

Hopefully this blog post has helped to show you how to easily set the first Chrome homepage tab while keeping your users’ existing tabs via script.  Download our f and give it a go yourself!

Leave a Reply

Your email address will not be published.