-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTweaks.ps1
425 lines (320 loc) · 13.7 KB
/
Tweaks.ps1
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
$script:BackedUpRegistryPaths = @()
$script:DisableBackups = $false
$script:RegistryTweaksDisabled = $false
$script:ScriptRunBackupDir = $null
$script:OKChar = [char]0x2714
$script:FailChar = [char]0x2716
$script:WarningChar = [char]0x26A0
function Test-IsAdminElevated {
return ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::
GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")
}
function Import-RegKeys {
param (
[ValidateNotNullOrEmpty()]
[string]$keyPath
)
if (!(Test-Path -Path $keyPath -PathType Container)) {
return
}
$regKeys = Get-ChildItem -Path $keyPath -Include *.reg -Recurse -ErrorAction SilentlyContinue
foreach ($key in $regKeys) {
if ($script:DisableBackups -eq $false) {
if (!(Export-RegKeys -KeyPath $key.FullName)) {
Write-Host "`t$script:FailChar " -NoNewline -ForegroundColor Red
Write-Host "`t$($key.Name): Failed to create registry backup."
continue
}
}
$result = reg import $key.FullName 2>&1
if ($LASTEXITCODE -ne 0) {
Write-Host "`t$script:FailChar $($key.Name): $($result -replace '^ERROR:\s*', '')" -ForegroundColor Red
} else {
Write-Host "`t$script:OKChar " -NoNewline -ForegroundColor Green
Write-Host $key.Name
}
}
}
function Export-RegKeys {
[CmdletBinding()]
param (
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[string]$KeyPath
)
if ($script:DisableBackups -eq $false -and !(Test-Path -Path $script:ScriptRunBackupDir)) {
return $false
}
$regFileContents = Get-Content -Path $KeyPath -ErrorAction SilentlyContinue
if ($regFileContents) {
$pattern = '\[(HKEY_[^\]]+)\]'
$patternMatches = [regex]::Matches($regFileContents, $pattern)
$patternMatches | ForEach-Object {
$keyRegPath = $_.Groups[1].Value
if($keyRegPath -notin $script:BackedUpRegistryPaths) {
$friendlyFileName = $keyRegPath -replace '^([A-Z]{3,4}):\\|\\', '$1-'
$fileNameParts = $friendlyFileName -split '-'
if ($fileNameParts.Count -ge 4) {
$friendlyFileName = "$($fileNameParts[0..1] -join '-')...$($fileNameParts[-2..-1] -join '-').reg"
} else {
$friendlyFileName = "${friendlyFileName}.reg"
}
$result = reg export $keyRegPath "$script:ScriptRunBackupDir\$friendlyFileName" /y 2>&1
if ($LASTEXITCODE -eq 0) {
$script:BackedUpRegistryPaths += $_.Groups[1].Value
return $true
} else {
return $false
}
} else {
return $true
}
}
}
return $false
}
if (!(Test-IsAdminElevated)) {
Write-Warning "Attempting to relaunch the script with elevated privileges..."
$scriptPath = $MyInvocation.MyCommand.Path
if (Get-Command wt -ErrorAction SilentlyContinue) {
$cmd = "wt"
$arguments = "new-tab -p `"PowerShell`" powershell -NoProfile -ExecutionPolicy Bypass -File `"$scriptPath`""
} else {
$cmd = "powershell"
$arguments = "-NoProfile -ExecutionPolicy Bypass -File `"$scriptPath`""
}
Start-Process $cmd -ArgumentList $arguments -Verb RunAs
exit
}
Clear-Host
# ==================== PREREQUISITES CHECK ====================
# Check for Windows 11.
if ([System.Environment]::OSVersion.Version.Build -lt 22000) {
throw "Windows 11 is required for this script to run."
}
# Check for whether to enable backups or not.
# Disable if running in Windows Sandbox as read-only access and... it's a sandbox.
if ([Environment]::UserName -eq 'WDAGUtilityAccount') {
$script:DisableBackups = $true
}
# ==================== SYSTEM UTILITIES ====================
Add-Type @"
using System;
using System.Runtime.InteropServices;
public class User32 {
[DllImport("user32.dll", SetLastError = true)]
public static extern bool SystemParametersInfo(uint action, uint param, IntPtr vparam, uint init);
}
public class RefreshDesktop
{
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
public const uint WM_KEYDOWN = 0x0100;
public const uint WM_KEYUP = 0x0101;
public const int VK_F5 = 0x74; // Virtual-key code for the F5 key
public static void Refresh()
{
IntPtr hWnd = FindWindow("Progman", "Program Manager");
if (hWnd == IntPtr.Zero)
{
throw new Exception("Could not find the desktop window.");
}
PostMessage(hWnd, WM_KEYDOWN, (IntPtr)VK_F5, IntPtr.Zero);
PostMessage(hWnd, WM_KEYUP, (IntPtr)VK_F5, IntPtr.Zero);
}
}
"@
# ==================== BACKUP INITIALISATION ====================
if ($script:DisableBackups -eq $false) {
Write-Host "[i] Initialising backup area..." -ForegroundColor Blue
# Initialise the backup folders.
$backupDir = "$PSScriptRoot\backups"
$scriptRunID = Get-date -Format 'dd-MM-yy_HH-mm-ss'
$script:ScriptRunBackupDir = "$backupDir\$scriptRunID"
# Create the backup dir.
$backupDirs = @(
$backupDir,
$script:ScriptRunBackupDir
)
foreach ($dir in $backupDirs) {
if (!(Test-Path $dir)) {
try {
New-Item -ItemType Directory -Path $dir -ErrorAction Stop | Out-Null
}
catch {
Write-Host "`t$script:FailChar Unable to create path: `"$dir`"." -ForegroundColor Red
Write-Host "`t$script:WarningChar Registry tweaks will be skipped." -ForegroundColor Yellow
$script:RegistryTweaksDisabled = $true
break
}
Write-Host "`t$script:OKChar " -NoNewline -ForegroundColor Green
Write-Host "Registry backup directory initialised:"
Write-Host "`t`t`"$script:ScriptRunBackupDir`""
}
}
}
# ==================== REGISTRY TWEAKS ====================
if ($script:RegistryTweaksDisabled -eq $false) {
Write-Host "[i] Starting registry tweaks..." -ForegroundColor Blue
Import-RegKeys -keyPath "$PSScriptRoot\assets\reg"
}
# ==================== SET APPROPRIATE POWER PLAN ====================
# Balanced for X3D, High Performance otherwise.
# Disable sleep whilst AC powered if target plan is balanced.
Write-Host '[i] Setting appropriate power plan...' -ForegroundColor Blue
$powerSchemes = & powercfg /list
if ($powerSchemes) {
$processorString = $null
$x3dCPU = $false
try {
$processorString = (Get-ItemProperty -Path "HKLM:\HARDWARE\DESCRIPTION\System\CentralProcessor\0" -Name "ProcessorNameString").ProcessorNameString
}
catch {
Write-Host "`t$script:WarningChar It was not possible to obtain the processor string." -ForegroundColor Yellow
}
$x3dCPU = if ($processorString -and $processorString -match "^AMD.*X3D") { $true } else { $false }
$targetPowerPlan = if ($x3dCPU) { "Balanced" } else { "High performance" }
$activeSchemeGUID = [regex]::Match($powerSchemes, 'Power Scheme GUID: ([a-f0-9-]+)\s+\([^\)]+\)\s*\*').Groups[1].Value
$desiredSchemeGUID = [regex]::Match($powerSchemes, "Power Scheme GUID: ([a-f0-9-]+)\s+\($targetPowerPlan\)").Groups[1].Value
# If the desired GUID was matched and the active power scheme is not our desired scheme.
if ($desiredSchemeGUID) {
if ($activeSchemeGUID -eq $desiredSchemeGUID) {
Write-Host "`t$script:OKChar " -NoNewline -ForegroundColor Green
Write-Host "Successfully applied $targetPowerPlan power plan."
} else {
# Set the desired scheme.
Write-Host "[i] Setting active power plan to: $targetPowerPlan" -ForegroundColor Blue
& powercfg /setactive $desiredSchemeGUID
if ($LASTEXITCODE -ne 0) {
Write-Host "`t$script:FailChar Failed to set $targetPowerPlan power plan."-ForegroundColor Red
} else {
Write-Host "`t$script:OKChar " -NoNewline -ForegroundColor Green
Write-Host "Successfully applied $targetPowerPlan power plan."
}
}
}
}
# ==================== ENABLE FEATURES ====================
Write-Host "[i] Processing Windows features..." -ForegroundColor Blue
# Apply Windows Firewall rules only outside of Windows Sandbox.
if ([Environment]::UserName -ne 'WDAGUtilityAccount') {
try {
Enable-NetFirewallRule -DisplayGroup "Remote Desktop"
Write-Host "`t$script:OKChar " -ForegroundColor Green -NoNewline
Write-Host "Allowed RDP in Windows Firewall."
}
catch {
Write-Host "`t$script:FailChar: $($_.Exception.Message)" -ForegroundColor Red
}
}
# ==================== REMOVE SHORTCUTS ====================
$publicShortcuts = @(
'Microsoft Edge.lnk'
)
if (@($publicShortcuts).Count -gt 0) {
Write-Host "[i] Processing Public Desktop shortcuts..." -ForegroundColor Blue
$publicShortcuts |
ForEach-Object { Join-Path "$env:SYSTEMDRIVE\Users\Public\Desktop" $_ } |
Where-Object { (Test-Path $_) -and $_ -match "\.lnk$" } |
ForEach-Object {
try {
$_ | Remove-Item -Force
Write-Host "`t$script:OKChar " -NoNewline -ForegroundColor Green
Write-Host "Removed `"$_`""
}
catch {
Write-Host "`t$script:FailChar Failed to remove `"$_`"" -ForegroundColor Red
}
}
}
# ==================== REMOVE ONEDRIVE ====================
# Standard OneDrive entries.
Write-Host "[i] Checking for OneDrive installations..." -ForegroundColor Blue
$oneDriveInstallations = @(
"HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall\OneDriveSetup.exe",
"HKCU:\Software\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\OneDriveSetup.exe",
"HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\OneDriveSetup.exe",
"HKLM:\Software\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\OneDriveSetup.exe"
) | Sort-Object -Unique | Where-Object {Test-Path $_}
if ($oneDriveInstallations) {
$oneDriveInstallations | ForEach-Object {
$uninstallString = Get-ItemPropertyValue -Path $_ -Name "UninstallString" -ErrorAction SilentlyContinue
if ($uninstallString){
Write-Host "[i] Executing: $uninstallString" -ForegroundColor Blue
Start-Process cmd -ArgumentList "/c $uninstallString" -Wait
}
}
} else {
Write-Host "`t$script:OKChar " -NoNewline -ForegroundColor Green
Write-Host "No OneDrive installations detected."
}
# Default user registry hive.
try {
$oneDriveKeyValue = "OneDriveSetup"
$defaultUserRunPath = "HKU:\TempDefault\Software\Microsoft\Windows\CurrentVersion\Run"
Write-Host "[i] Checking the default user's registry hive for $oneDriveKeyValue..." -ForegroundColor Blue
$hkuDrive = New-PSDrive -Name HKU -PSProvider Registry -Root HKEY_USERS -ErrorAction Stop
$null = reg load HKU\TempDefault C:\users\Default\NTUSER.DAT 2>&1
if ($LASTEXITCODE -ne 0) {
throw "Failed to load the default user's registry hive."
}
$hiveLoaded = $true
$oneDriveDefaultUserSetup = Get-ItemProperty -Path $defaultUserRunPath -Name $oneDriveKeyValue -ErrorAction SilentlyContinue
if ($oneDriveDefaultUserSetup) {
try {
Write-Host "[i] Removing $oneDriveKeyValue from $($defaultUserRunPath -replace "HKU:", "HKEY_USERS")" -ForegroundColor Blue
$oneDriveDefaultUserSetup | Remove-ItemProperty -Name $oneDriveKeyValue -Force
Write-Host "`t$script:OKChar " -NoNewline -ForegroundColor Green
Write-Host "Registry key removed."
}
catch {
throw "Failed to remove $oneDriveKeyValue"
}
} else {
Write-Host "`t$script:OKChar " -NoNewline -ForegroundColor Green
Write-Host "No `"$oneDriveKeyValue`" detected in the default user's registry hive."
}
}
catch {
Write-Host "`t$script:FailChar $_" -ForegroundColor Red
}
finally {
if ($hkuDrive) {
Remove-PSDrive -Name HKU
}
if ($hiveLoaded) {
$null = reg unload HKU\TempDefault 2>&1
}
}
# ==================== RESURRECT EXPLORER ====================
Write-Host '[i] Restarting explorer...' -ForegroundColor Blue
Stop-Process -Name explorer -Force
while (!(Get-Process -Name "explorer" -ErrorAction SilentlyContinue)) {
Start-Sleep -Milliseconds 500
}
Write-Host "`t$script:OKChar " -NoNewline -ForegroundColor Green
Write-Host "Explorer restarted."
# ==================== APPLY WALLPAPER CHANGES ====================
Write-Host "[i] Applying wallpaper..." -ForegroundColor Blue
$SPI_SETDESKWALLPAPER = 0x0014
$SPIF_UPDATEINIFILE = 0x01
$SPIF_SENDCHANGE = 0x02
$result = [User32]::SystemParametersInfo($SPI_SETDESKWALLPAPER, 0, [IntPtr]::Zero, $SPIF_UPDATEINIFILE -bor $SPIF_SENDCHANGE)
if ($result) {
Write-Host "`t$script:OKChar " -NoNewline -ForegroundColor Green
Write-Host "Wallpaper applied."
} else {
Write-Host "`t$script:FailChar " -NoNewline -ForegroundColor Red
Write-Host "Failed to apply wallpaper."
}
# ==================== REFRESH DESKTOP ====================
while ([RefreshDesktop]::FindWindow("Progman", "Program Manager") -eq [IntPtr]::Zero) {
Start-Sleep -Milliseconds 500
continue
}
Write-Host "[i] Refreshing desktop..." -ForegroundColor Blue
[RefreshDesktop]::Refresh()
Write-Host "[i] Done."