-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreate-EdgeProfile.ps1
More file actions
345 lines (311 loc) · 10.3 KB
/
Create-EdgeProfile.ps1
File metadata and controls
345 lines (311 loc) · 10.3 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
<#
.DESCRIPTION
This script creates a new Edge profile with the specified name and settings.
.PARAMETER Name
This parameter is a string and is mandatory.
The name of the new Edge profile.
.PARAMETER Image
This parameter is a string and is optional.
The URL of the image to use as the icon for the new Edge profile.
.PARAMETER StartMenuShortcut
This parameter is a switch and is optional and requires administrator privileges.
Creates a Start Menu shortcut for the new Edge profile.
.EXAMPLE
Create-EdgeProfile -Name "Example"
This example creates a new Edge profile named "Example".
.EXAMPLE
Create-EdgeProfile -Name "Example" -Image "https://example.com/image.png"
This example creates a new Edge profile named "Example" with the specified image as the icon.
.EXAMPLE
Create-EdgeProfile -Name "Example" -Image "https://example.com/image.png" -StartMenuShortcut
This example creates a new Edge profile named "Example" with the specified image as the icon and creates a Start Menu shortcut for the profile.
.EXAMPLE
Create-EdgeProfile -Name "Example" -StartMenuShortcut
This example creates a new Edge profile named "Example" and creates a Start Menu shortcut for the profile.
.NOTES
1.0 - 2024-06-27 - Initial version.
#>
# Parameters
param(
[Parameter(Mandatory = $true)]
[ValidatePattern("^[a-zA-Z0-9]+$")]
[string]$Name,
[Parameter(Mandatory = $false)]
[string]$Image,
[Parameter(Mandatory = $false)]
[switch]$StartMenuShortcut
)
# Variables
$ProfileFolder = "Profile-" + $Name
$ProfilePath = "$Env:LOCALAPPDATA\Microsoft\Edge\User Data\$ProfileFolder"
$EdgePath = "C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe"
# Check if script is running as administrator
if ($StartMenuShortcut) {
if (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
Write-Warning "You need to run this script as an administrator to create a Start Menu shortcut."
Break
}
}
# Check if profile already exists
if ((Test-Path $ProfilePath) -eq $true) {
Write-Error "An Edge profile already exists with this name. Try another one."
Break
}
# Create profile
try {
Start-Process -FilePath $EdgePath -ArgumentList "--profile-directory=$ProfileFolder --no-first-run --no-default-browser-check --flag-switches-begin --flag-switches-end --site-per-process" | Out-Null
}
catch {
Write-Error "Failed to start Edge, make sure you have Edge installed in the default location."
Break
}
# Wait for Edge to create the profile
Write-Output "Waiting for Edge to create the profile..."
Start-Sleep -Seconds 15
# Close Edge
try {
Stop-Process -Name "msedge"
}
catch {
Write-Error "Failed to close Edge. Make sure to not close Edge manually while the script is running."
Break
}
# Backup Local State
try {
$LocalStateFile = "$Env:LOCALAPPDATA\Microsoft\Edge\User Data\Local State"
$LocalStateBackUp = "$Env:LOCALAPPDATA\Microsoft\Edge\User Data\Local State Backup"
Copy-Item $LocalStateFile -Destination $LocalStateBackUp
}
catch {
Write-Error "Failed to backup Local State. Make sure that `"$LocalStateFile`" exists."
Break
}
# Write profile name to Local State
try {
$State = Get-Content -Raw $LocalStateFile
$Json = $State | ConvertFrom-Json
$NewEdgeProfile = $Json.profile.info_cache.$ProfileFolder
$NewEdgeProfile.name = $Name
$Json | ConvertTo-Json -Compress -Depth 100 | Out-File $LocalStateFile -Encoding UTF8
}
catch {
Write-Error "Failed to write profile name to Local State."
Break
}
# Write profile name to registry
try {
Push-Location
Set-Location HKCU:\Software\Microsoft\Edge\Profiles\$ProfileFolder
Set-ItemProperty . ShortcutName "$Name"
Pop-Location
}
catch {
Write-Error "Failed to write profile name to registry."
Break
}
# Backup Preferences
try {
$PreferencesSettings = "$ProfilePath\Preferences"
$PreferencesSettingsBackup = "$ProfilePath\Preferences Backup"
Copy-Item $PreferencesSettings -Destination $PreferencesSettingsBackup
}
catch {
Write-Error "Failed to backup Preferences. Make sure that `"$ProfilePath\Preferences`" exists."
Break
}
# Load Preferences
try {
$Preferencess = Get-Content -Raw $PreferencesSettings
$PreferencesJson = $Preferencess | ConvertFrom-Json
}
catch {
Write-Error "Failed to load Preferences. Make sure that `"$ProfilePath\Preferences`" exists."
Break
}
# Disable Sidebar
if ($null -eq $PreferencesJson.browser.show_hub_apps_tower) {
try {
$PreferencesJson.browser | add-member -Name "show_hub_apps_tower" -value $false -MemberType NoteProperty
}
catch {
Write-Error "Failed to add show_hub_apps_tower to Preferences."
Break
}
}
else {
try {
$PreferencesJson.browser.show_hub_apps_tower = $false
}
catch {
Write-Error "Failed to set show_hub_apps_tower to false in Preferences."
Break
}
}
if ($null -eq $PreferencesJson.browser.show_hub_apps_tower_pinned) {
try {
$PreferencesJson.browser | add-member -Name "show_hub_apps_tower_pinned" -value $false -MemberType NoteProperty
}
catch {
Write-Error "Failed to add show_hub_apps_tower_pinned to Preferences."
Break
}
}
else {
try {
$PreferencesJson.browser.show_hub_apps_tower_pinned = $false
}
catch {
Write-Error "Failed to set show_hub_apps_tower_pinned to false in Preferences."
Break
}
}
if ($null -eq $PreferencesJson.browser.show_toolbar_learning_toolkit_button) {
try {
$PreferencesJson.browser | add-member -Name "show_toolbar_learning_toolkit_button" -value $false -MemberType NoteProperty
}
catch {
Write-Error "Failed to add show_toolbar_learning_toolkit_button to Preferences."
Break
}
}
else {
try {
$PreferencesJson.browser.show_toolbar_learning_toolkit_button = $false
}
catch {
Write-Error "Failed to set show_toolbar_learning_toolkit_button to false in Preferences."
Break
}
}
# Disable data share between profiles
if ($null -eq $PreferencesJson.local_browser_data_share.enabled) {
$Value = @"
{
"enabled": false,
"index_last_cleaned_time": "0"
}
"@
try {
$PreferencesJson | add-member -Name "local_browser_data_share" -value (Convertfrom-Json $Value) -MemberType NoteProperty
}
catch {
Write-Error "Failed to add local_browser_data_share to Preferences."
Break
}
}
else {
try {
$PreferencesJson.local_browser_data_share.enabled = $false
}
catch {
Write-Error "Failed to disable local_browser_data_share in Preferences."
Break
}
}
# Disable account based profile switching
if ($null -eq $PreferencesJson.guided_switch.enabled) {
$Value = @"
{
"enabled": false
}
"@
try {
$PreferencesJson | add-member -Name "guided_switch" -value (Convertfrom-Json $Value) -MemberType NoteProperty
}
catch {
Write-Error "Failed to add guided_switch to Preferences."
Break
}
}
else {
try {
$PreferencesJson.guided_switch.enabled = $false
}
catch {
Write-Error "Failed to disable guided_switch in Preferences."
Break
}
}
# Edit start page settings
$Value = @"
{
"background_image_type":"imageAndVideo",
"hide_default_top_sites":false,
"layout_mode":3,
"news_feed_display":"off",
"num_personal_suggestions":1,
"prerender_contents_height":823,
"prerender_contents_width":1185,
"quick_links_options":0
}
"@
if ($null -eq $PreferencesJson.ntp) {
try {
$PreferencesJson | add-member -Name "ntp" -value (Convertfrom-Json $Value) -MemberType NoteProperty
}
catch {
Write-Error "Failed to add ntp to Preferences."
Break
}
}
else {
try {
$PreferencesJson | add-member -Name "ntp" -value (Convertfrom-Json $Value) -MemberType NoteProperty -Force
}
catch {
Write-Error "Failed to set ntp in Preferences."
Break
}
}
# Write new settings to preferences
$PreferencesJson | ConvertTo-Json -Compress -Depth 100 | Out-File $PreferencesSettings -Encoding UTF8
if ($Image -ne "") {
# Download image
try {
Invoke-WebRequest -Uri $Image -OutFile "$ProfilePath\TempImage-$Name.png" -ErrorAction Stop
}
catch {
Write-Error "Failed to download image. Make sure the URL is correct and accessible."
Break
}
# Save icon from image and cleanup
try {
Add-Type -AssemblyName System.Windows.Forms, System.Drawing
$LoadedImage = [Drawing.Image]::FromFile("$ProfilePath\TempImage-$Name.png")
$IntPtr = New-Object IntPtr
$Thumbnail = $LoadedImage.GetThumbnailImage(72, 72, $null, $IntPtr)
$Bitmap = New-Object Drawing.Bitmap $Thumbnail
$Bitmap.SetResolution(72, 72);
$Icon = [System.Drawing.Icon]::FromHandle($Bitmap.GetHicon());
$File = [IO.File]::Create("$ProfilePath\Edge Profile.ico")
$Icon.Save($File)
$File.Close()
$Icon.Dispose()
$Bitmap.Dispose()
$LoadedImage.Dispose()
$Thumbnail.Dispose()
Remove-Item "$ProfilePath\TempImage-$Name.png" -Force
}
catch {
Write-Error "Failed to save icon from image. Make sure the image is a valid."
Break
}
}
if ($StartMenuShortcut) {
# Create Start Menu shortcut
try {
$TargetPath = $EdgePath
$ShortcutFile = "C:\ProgramData\Microsoft\Windows\Start Menu\$Name - Edge.lnk"
$WScriptShell = New-Object -ComObject WScript.Shell
$Shortcut = $WScriptShell.CreateShortcut($ShortcutFile)
$Shortcut.IconLocation = "$ProfilePath\Edge Profile.ico, 0"
$Shortcut.Arguments = "--profile-directory=`"$ProfileFolder`""
$Shortcut.TargetPath = $TargetPath
$Shortcut.Save()
}
catch {
Write-Error "Failed to create Start Menu shortcut."
Break
}
}
Write-Output "Successfully created Edge profile `"$Name`"."