Functions/GenXdev.Webbrowser/Export-BrowserBookmarks.ps1

<##############################################################################
Part of PowerShell module : GenXdev.Webbrowser
Original cmdlet filename : Export-BrowserBookmarks.ps1
Original author : René Vaessen / GenXdev
Version : 1.300.2025
################################################################################
Copyright (c) René Vaessen / GenXdev
 
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
 
    http://www.apache.org/licenses/LICENSE-2.0
 
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
################################################################################>

###############################################################################
<#
.SYNOPSIS
Exports browser bookmarks to a JSON file.
 
.DESCRIPTION
The Export-BrowserBookmarks cmdlet exports bookmarks from a specified web browser
(Microsoft Edge, Google Chrome, or Mozilla Firefox) to a JSON file. Only one
browser type can be specified at a time. The bookmarks are exported with full
preservation of their structure and metadata.
 
.PARAMETER OutputFile
The path to the JSON file where the bookmarks will be saved. The path will be
expanded to a full path before use.
 
.PARAMETER Chrome
Switch parameter to export bookmarks from Google Chrome browser.
 
.PARAMETER Edge
Switch parameter to export bookmarks from Microsoft Edge browser.
 
.PARAMETER Firefox
Switch parameter to export bookmarks from Mozilla Firefox browser.
 
.EXAMPLE
Export-BrowserBookmarks -OutputFile "C:\MyBookmarks.json" -Edge
 
.EXAMPLE
Export-BrowserBookmarks "C:\MyBookmarks.json" -Chrome
#>

function Export-BrowserBookmarks {

    [CmdletBinding()]
    [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseSingularNouns', '')]
    [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidGlobalVars', '')]
    [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '')]
    param (
        ########################################################################
        [Parameter(
            Mandatory = $true,
            Position = 0,
            HelpMessage = 'Path to the JSON file where bookmarks will be saved'
        )]
        [ValidateNotNullOrEmpty()]
        [string]$OutputFile,
        ########################################################################
        [Parameter(
            Mandatory = $false,
            HelpMessage = 'Export bookmarks from Google Chrome'
        )]
        [switch]$Chrome,
        ########################################################################
        [Parameter(
            Mandatory = $false,
            HelpMessage = 'Export bookmarks from Microsoft Edge'
        )]
        [switch]$Edge,
        ########################################################################
        [Parameter(
            Mandatory = $false,
            ParameterSetName = 'Firefox',
            HelpMessage = 'Export bookmarks from Mozilla Firefox'
        )]
        [switch]$Firefox
        ########################################################################
    )

    begin {
        # convert relative or partial path to full filesystem path
        $outputFilePath = GenXdev.FileSystem\Expand-Path $OutputFile

        # inform user about the output destination
        Microsoft.PowerShell.Utility\Write-Verbose "Exporting bookmarks to: $outputFilePath"
    }


    process {

        # initialize empty hashtable for browser selection parameters
        $bookmarksArguments = @{}

        # set appropriate flag based on selected browser type
        if ($Chrome) {
            $bookmarksArguments['Chrome'] = $true
            Microsoft.PowerShell.Utility\Write-Verbose 'Exporting Chrome bookmarks'
        }
        if ($Edge) {
            $bookmarksArguments['Edge'] = $true
            Microsoft.PowerShell.Utility\Write-Verbose 'Exporting Edge bookmarks'
        }
        if ($Firefox) {
            $bookmarksArguments['Firefox'] = $true
            Microsoft.PowerShell.Utility\Write-Verbose 'Exporting Firefox bookmarks'
        }

        # retrieve bookmarks and save them as formatted json to the output file
        GenXdev.Webbrowser\Get-BrowserBookmark @bookmarksArguments |
            Microsoft.PowerShell.Utility\ConvertTo-Json -Depth 100 |
            Microsoft.PowerShell.Management\Set-Content -LiteralPath $outputFilePath -Force

        Microsoft.PowerShell.Utility\Write-Verbose 'Bookmarks exported successfully'
    }

    end {
    }
}