Script to add bookmarks to Chrome

I am new to Powershell. I want to create a script to add specific bookmarks to a user’s Chrome browser when setting up their account with them. I would put it on a USB drive and share it with my team.

I have found a script that does this and created an EXE with it but I need to know how to add not just one bookmark but multiple. I can see somewhat is going on with the script but I’m not familiar enough to understand enough to add more to the script to add multiple bookmarks.

$fileBookmarks="$env:LOCALAPPDATA\Google\Chrome\User Data\Default\Bookmarks"
$dataBookmarks=Get-Content $fileBookmarks -Encoding UTF8| Out-String |ConvertFrom-Json
function createNewChromeBookmark ($Bookmarks, [string]$BookmarkName, [string]$BookmarkURL) {
    function getBookmarkIDs($object){
        $object | ForEach-Object{
            "{0:0000}" -f [int]($_.id);
            if([bool]($_.PSobject.Properties.name -match "children")){
                GetBookmarkIDs($_.children);
            }
        }
    };
    $nextBookmarkID = [int](getBookmarkIDs -object $Bookmarks.roots.bookmark_bar|Sort-Object -Descending|Select-Object -First 1) + 1
    $currentChomeTime=[System.DateTimeOffset]::Now.ToUnixTimeMilliseconds()*10000;
    $newBookmark= [PSCustomObject]@{
        date_added=$currentChomeTime
        guid=[guid]::NewGuid()
        id=$nextBookMarkID
        name="$BookmarkName"
        type="url"
        url="$BookmarkURL"
    }
    return $newBookmark;
}
$newBookmark = createNewChromeBookmark -Bookmarks $dataBookmarks -BookmarkName "Google" -BookmarkURL "https://www.google.com/";
$dataBookmarks.roots.bookmark_bar.children += $newBookmark;
$dataBookmarksJSON = ConvertTo-Json -InputObject $dataBookmarks -Depth 100
Set-Content -Path $fileBookmarks -Value $dataBookmarksJSON -Encoding UTF8

PatM,
Welcome to the forum. :wave:t4:

This forum is more about questions you have with code you wrote by yourself than reviewing, customizing or enhancing scripts you’ve found on the internet.

I’d recommend to ask the author of the script for help.

The easiest solution would be to run the script multiple times. :wink:

It’s not that hard …

The first 2 lines read the bookmarks file.
Line 3 to 23 define the needed functions
Line 24 creates a new bookmark for the URL “https://www.google.com/” with the name “Google”
Line 25 adds this newly created bookmark to the already existing bookmarks in the variable $dataBookmarks
Line 26 converts the PS objects containing all the bookmarks to a string representation in the format JSON.
And the last line writes all this back to the disk.

Now it’s up to you to decide what lines you have to duplicate to add more than one bookmark. :wink:

Thank you

I have an idea of what needs to be done as far as doing it again for another bookmark it’s just knowing the syntax needed to make it work.

I appreciate your input.

As far as the forum being for scripts I’ve written myself I understand. Is there another forum area here that would be more appropriate or do you have any suggestions for forums elsewhere that would?

Just looking for help and I appreciate your input.

As far as I’m concerned all free peer to peer forums focus on helping you helping yourself. And if you carefully read my first answer you have actually all you need. :wink: Have you concidered asking the author of the script for help?

I understand and I do want to learn PowerShell. I kind of understand what’s happening in the script and I will be looking for what I need to do to add another bookmark. I was just wanting a little help but I will work on it because I want to fully understand how it works and be able to write scripts myself.

I will read over your reply again and work on it.
Thank you

I figured it out. It was way easier than I thought it would be. I was just overthinking it and expecting it to be harder.

As far as the person who wrote the script the last time he was on that forum was 2 years ago so I didn’t think he would respond.

Again, Thank you for the input!

Great. :+1:t4: I’m glad you’ve found a solution. :slightly_smiling_face:

How about sharing your solution here? This could help others looking for the same or a similar problem in the future.

Thanks in advance.

hi i was wondering if you where able to share your script that added multiple Bookmarks as i cannot find it online anywhere please