-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathset-default-browser.ps1
More file actions
109 lines (98 loc) · 3.39 KB
/
set-default-browser.ps1
File metadata and controls
109 lines (98 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<#
.SYNOPSIS
Sets default browser for web-protocols
Invoke with "./set-default-browser <browsername>"
.NOTES
2017-01-17/SDAA
#>
param (
[string]$browser = "default"
)
function Set-DefaultBrowser
{
param($defaultBrowser)
$regKey = "HKCU:\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\{0}\UserChoice"
$regKeyFtp = $regKey -f 'ftp'
$regKeyHttp = $regKey -f 'http'
$regKeyHttps = $regKey -f 'https'
switch -Regex ($defaultBrowser.ToLower())
{
# Internet Explorer
'ie|internet|explorer' {
Set-ItemProperty $regKeyFtp -name ProgId IE.FTP
Set-ItemProperty $regKeyHttp -name ProgId IE.HTTP
Set-ItemProperty $regKeyHttps -name ProgId IE.HTTPS
break
}
# Firefox
'ff|firefox' {
Set-ItemProperty $regKeyFtp -name ProgId FirefoxURL
Set-ItemProperty $regKeyHttp -name ProgId FirefoxURL
Set-ItemProperty $regKeyHttps -name ProgId FirefoxURL
break
}
# Google Chrome
'cr|google|chrome' {
Set-ItemProperty $regKeyFtp -name ProgId ChromeHTML
Set-ItemProperty $regKeyHttp -name ProgId ChromeHTML
Set-ItemProperty $regKeyHttps -name ProgId ChromeHTML
break
}
# Safari
'sa*|apple' {
Set-ItemProperty $regKeyFtp -name ProgId SafariURL
Set-ItemProperty $regKeyHttp -name ProgId SafariURL
Set-ItemProperty $regKeyHttps -name ProgId SafariURL
break
}
# Opera
'op*' {
Set-ItemProperty $regKeyFtp -name ProgId Opera.Protocol
Set-ItemProperty $regKeyHttp -name ProgId Opera.Protocol
Set-ItemProperty $regKeyHttps -name ProgId Opera.Protocol
break
}
}
}
# thanks to http://newoldthing.wordpress.com/2007/03/23/how-does-your-browsers-know-that-its-not-the-default-browser/
# Errorhandling tips: https://blogs.technet.microsoft.com/heyscriptingguy/2015/04/03/catch-powershell-errors-related-to-reading-the-registry/
function create-reg-keys {
# If there is no keys present, create them
$ErrorActionPreference = "stop"
try {
(Get-ItemProperty 'HKCU:\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\ftp\UserChoice').ProgId
}
catch [System.Management.Automation.ItemNotFoundException] {
New-Item 'HKCU:\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\ftp' -force|Out-Null
New-Item 'HKCU:\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\ftp\UserChoice' -force|Out-Null
}
finally {
$ErrorActionPreference = "Continue"
}
}
<#Try {
(Get-ItemProperty 'HKCU:\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice').ProgId
}
catch [System.Management.Automation.ItemNotFoundException] {
New-Item $regKeyHttp
}
Try {
(Get-ItemProperty 'HKCU:\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\https\UserChoice').ProgId
}
catch [System.Management.Automation.ItemNotFoundException] {
New-Item $regKeyHttps
}
#>
if ($browser -eq "default") {
Write-Host "Usage: ./set-default-browser -browser <ff|ie|cr|op|sa>"
}
else {
Write-Host "In the else $browser"
create-reg-keys
Set-DefaultBrowser $browser
}
# Set-DefaultBrowser cr
# Set-DefaultBrowser ff
# Set-DefaultBrowser ie
# Set-DefaultBrowser op
# Set-DefaultBrowser sa