Created
October 16, 2024 06:44
-
-
Save 0liver/505c27090e730b93fdc50273abc20281 to your computer and use it in GitHub Desktop.
PowerShell script to set default browser in Windows 10
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function duck { set-browser duck } | |
function edge { set-browser edge } | |
function set-browser { | |
param([string] $name) | |
$browserPositionMap = @{ | |
chrome = "{TAB 2}" | |
duck = "" | |
edge = "{TAB 4}" | |
firefox = "{TAB 1}" | |
opera = "{TAB 5}" | |
} | |
if (-not $browserPositionMap.ContainsKey($name)) { | |
Write-Error "\"$name\" is not a known browser name" | |
} | |
Add-Type -AssemblyName "System.Windows.Forms" | |
Write-Host "Opening settings app" | |
Start-Process ms-settings:defaultapps | |
Write-Host "Waiting..." | |
Start-Sleep 2 # wait for the settings to open | |
# tabs down to default browser, & selects the first one, would have to enter {DOWN} however many times to select another browser, its in alphabatical format | |
Write-Host "Tabbing down to Default Browser" | |
[System.Windows.Forms.SendKeys]::SendWait("{TAB 5}") | |
# Sending a space key to open the choices dialog. | |
[System.Windows.Forms.SendKeys]::SendWait(" ") | |
Write-Host "Waiting..." | |
Start-Sleep 1 # wait for choices to appear | |
Write-Host "Choosing $name from the list" | |
[System.Windows.Forms.SendKeys]::SendWait($browserPositionMap[$name]) | |
Write-Host "Waiting before confirming..." | |
Start-Sleep 1 # wait for choices to appear | |
[System.Windows.Forms.SendKeys]::SendWait("{ENTER}") | |
Write-Host "Waiting 2s before exiting..." | |
Start-Sleep 2 # wait 2sec then press Alt+F4 to close Settings. | |
[System.Windows.Forms.SendKeys]::SendWait("%{F4}") | |
Write-Host "Done." | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment