Functions/GenXdev.Webbrowser/Get-DefaultWebbrowser.cs

using System.Collections;
using System.Management.Automation;
using Microsoft.Win32;

namespace GenXdev.Webbrowser
{
    [System.ComponentModel.Description(@"
.SYNOPSIS
Returns the configured default web browser for the current user.
.DESCRIPTION
* Retrieves information about the system's default web browser by querying
  the Windows Registry.
* Returns a hashtable containing the browser's name, description, icon
  path, and executable path.
* The function checks both user preferences and system-wide browser
  registrations to determine the default browser.

.LICENSE
Copyright (C) 2026 René Vaessen / GenXdev

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.

.EXAMPLE
```powershell
Get-DefaultWebbrowser | Format-List
```

Get detailed information about the default browser.
.EXAMPLE
```powershell
$browser = Get-DefaultWebbrowser
& $browser.Path https://www.github.com/
```

Launch the default browser with a specific URL.
")]
    [Cmdlet(VerbsCommon.Get, "DefaultWebbrowser")]
    [OutputType(typeof(Hashtable))]
    public class GetDefaultWebbrowserCommand : PSGenXdevCmdlet
    {
        private string urlHandlerId;

        /// <summary>
        /// Begin processing - retrieve default browser URL handler configuration
        /// </summary>
        protected override void BeginProcessing()
        {
            // Define registry paths for url associations and browser information
            string urlAssocPath = @"SOFTWARE\Microsoft\Windows\Shell\Associations\UrlAssociations\https\UserChoice";

            WriteVerbose("Retrieving default browser URL handler configuration");

            // Get the default handler ID for HTTPS URLs from user preferences
            using (RegistryKey hkcu = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Default))
            {
                using (RegistryKey urlAssocKey = hkcu.OpenSubKey(urlAssocPath))
                {
                    if (urlAssocKey != null)
                    {
                        urlHandlerId = urlAssocKey.GetValue("ProgId") as string;
                    }
                }
            }

            WriteVerbose($"URL handler ID: {urlHandlerId}");
        }

        /// <summary>
        /// Process record - scan installed browsers and return default browser info
        /// </summary>
        protected override void ProcessRecord()
        {
            if (string.IsNullOrEmpty(urlHandlerId))
            {
                return;
            }

            WriteVerbose("Scanning installed browsers in registry");

            // Iterate through all registered browsers in the system
            using (RegistryKey browsersKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\WOW6432Node\Clients\StartMenuInternet"))
            {
                if (browsersKey == null)
                {
                    return;
                }

                foreach (string browserName in browsersKey.GetSubKeyNames())
                {
                    using (RegistryKey browserRoot = browsersKey.OpenSubKey(browserName))
                    {
                        if (browserRoot == null)
                        {
                            continue;
                        }

                        // Construct the full registry path for the current browser
                        string browserRootPath = @"SOFTWARE\WOW6432Node\Clients\StartMenuInternet\" + browserName;

                        // Verify browser has required registry keys for URL handling
                        using (RegistryKey commandKey = browserRoot.OpenSubKey(@"shell\open\command"))
                        {
                            using (RegistryKey urlAssocKey = browserRoot.OpenSubKey(@"Capabilities\URLAssociations"))
                            {
                                if (commandKey == null || urlAssocKey == null)
                                {
                                    continue;
                                }

                                // Get the HTTPS handler ID for this browser
                                string browserHandler = urlAssocKey.GetValue("https") as string;

                                // Check if this browser is the default handler
                                if (browserHandler == urlHandlerId)
                                {
                                    WriteVerbose($"Found default browser: {browserRootPath}");

                                    // Get browser details
                                    using (RegistryKey capabilities = browserRoot.OpenSubKey("Capabilities"))
                                    {
                                        if (capabilities == null)
                                        {
                                            continue;
                                        }

                                        string name = capabilities.GetValue("ApplicationName") as string;
                                        string description = capabilities.GetValue("ApplicationDescription") as string;
                                        string icon = capabilities.GetValue("ApplicationIcon") as string;
                                        string path = (commandKey.GetValue(null) as string)?.Trim('"');

                                        // Return browser details in a hashtable
                                        Hashtable result = new Hashtable
                                        {
                                            ["Name"] = name,
                                            ["Description"] = description,
                                            ["Icon"] = icon,
                                            ["Path"] = path
                                        };

                                        WriteObject(result);
                                        return;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        /// <summary>
        /// End processing - no cleanup needed
        /// </summary>
        protected override void EndProcessing()
        {
        }
    }
}