forked from Upinel/BetterRDP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBetterRDP.ps1
459 lines (404 loc) · 17 KB
/
BetterRDP.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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
# BetterRDP PowerShell Script
# Original Author: Nova Upinel Chow <[email protected]>
# License: Apache 2.0
# Source: https://github.com/Upinel/BetterRDP
# References for optimizations:
# - Flow Control & System Responsiveness settings:
# https://www.reddit.com/r/killerinstinct/comments/4fcdhy/an_excellent_guide_to_optimizing_your_windows_10/
# - DWM Frame Interval setting:
# https://support.microsoft.com/en-us/help/2885213/frame-rate-is-limited-to-30-fps-in-windows-8-and-windows-server-2012-r
class RegistryState {
[string]$Path
[string]$Name
[object]$Value
[string]$Type
[bool]$Exists
[bool]$ParentExists
}
function Get-RegistryState {
param (
[Parameter(Mandatory=$true)]
[string]$Path,
[Parameter(Mandatory=$true)]
[string]$Name
)
$state = [RegistryState]::new()
$state.Path = $Path
$state.Name = $Name
# Debug info
Write-Host "Checking path: $Path" -ForegroundColor DarkGray
$regPath = $Path -replace 'HKLM:\\', 'HKLM\'
Write-Host "Converted path: $regPath" -ForegroundColor DarkGray
# Store reg.exe output for inspection
$regOutput = reg.exe query $regPath 2>&1
$state.ParentExists = $LASTEXITCODE -eq 0
Write-Host "reg.exe exit code: $LASTEXITCODE" -ForegroundColor DarkGray
Write-Host "reg.exe output: $regOutput" -ForegroundColor DarkGray
Write-Host "ParentExists: $($state.ParentExists)" -ForegroundColor DarkGray
if ($state.ParentExists) {
$property = Get-ItemProperty -Path $Path -Name $Name -ErrorAction SilentlyContinue
$state.Exists = $null -ne $property
if ($state.Exists) {
$state.Value = $property.$Name
$state.Type = (Get-ItemProperty -Path $Path -Name $Name).PSObject.Properties[$Name].TypeNameOfValue
}
}
return $state
}
function Backup-RegistrySettings {
$backupFile = ".\rdp_settings_backup.json"
$backup = @{}
$settings = @{
"HKLM:\SOFTWARE\Policies\Microsoft\Windows NT\Terminal Services" = @(
"SelectTransport",
"fEnableVirtualizedGraphics",
"fEnableRemoteFXAdvancedRemoteApp",
"MaxCompressionLevel",
"VisualExperiencePolicy",
"GraphicsProfile",
"bEnumerateHWBeforeSW",
"AVC444ModePreferred",
"AVCHardwareEncodePreferred",
"VGOptimization_CaptureFrameRate",
"VGOptimization_CompressionRatio",
"ImageQuality"
)
"HKLM:\SYSTEM\CurrentControlSet\Services\TermDD" = @(
"FlowControlDisable",
"FlowControlDisplayBandwidth",
"FlowControlChannelBandwidth",
"FlowControlChargePostCompression"
)
"HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Multimedia\SystemProfile" = @(
"SystemResponsiveness"
)
"HKLM:\SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations" = @(
"DWMFRAMEINTERVAL"
)
"HKLM:\SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp" = @(
"InteractiveDelay"
)
"HKLM:\SYSTEM\CurrentControlSet\Services\LanmanWorkstation\Parameters" = @(
"DisableBandwidthThrottling",
"DisableLargeMtu"
)
}
foreach ($path in $settings.Keys) {
$backup[$path] = @{}
foreach ($name in $settings[$path]) {
$backup[$path][$name] = Get-RegistryState -Path $path -Name $name
}
}
$backup | ConvertTo-Json -Depth 10 | Set-Content $backupFile
return $backupFile
}
function Validate-Backup {
param (
[Parameter(Mandatory=$true)]
[string]$BackupFile
)
if (-not (Test-Path $BackupFile)) {
Write-Error "Backup file not found!"
return $false
}
try {
$null = Get-Content $BackupFile | ConvertFrom-Json
return $true
} catch {
Write-Error "Invalid backup file format!"
return $false
}
}
function Restore-RegistrySettings {
param (
[Parameter(Mandatory=$true)]
[string]$BackupFile
)
$backup = Get-Content $BackupFile | ConvertFrom-Json
$typeMap = @{
'System.Int32' = 'DWord'
'System.Int64' = 'QWord'
'System.String' = 'String'
'System.String[]' = 'MultiString'
'System.Byte[]' = 'Binary'
}
foreach ($pathObj in $backup.PSObject.Properties) {
$path = $pathObj.Name
$settings = $pathObj.Value
foreach ($nameObj in $settings.PSObject.Properties) {
$state = $nameObj.Value
if (-not $state.ParentExists) {
# Instead of skipping, check if value exists now and delete it
if (Test-Path $state.Path) {
$existing = Get-ItemProperty -Path $state.Path -Name $state.Name -ErrorAction SilentlyContinue
if ($null -ne $existing) {
Remove-ItemProperty -Path $state.Path -Name $state.Name -Force
Write-Host "Removed $($state.Path)\$($state.Name) as it did not exist in backup" -ForegroundColor Yellow
}
}
continue
}
if ($state.Exists -and $null -ne $state.Value) {
if (-not (Test-Path $state.Path)) {
New-Item -Path $state.Path -Force | Out-Null
}
$regType = if ($state.Type -and $typeMap.ContainsKey($state.Type)) {
$typeMap[$state.Type]
} else {
Write-Warning "Unknown type $($state.Type) for $($state.Path)\$($state.Name), defaulting to DWord"
'DWord'
}
Set-ItemProperty -Path $state.Path -Name $state.Name -Value $state.Value -Type $regType
Write-Host "Restored $($state.Path)\$($state.Name) to $($state.Value)" -ForegroundColor Green
}
else {
if ((Test-Path $state.Path)) {
$existing = Get-ItemProperty -Path $state.Path -Name $state.Name -ErrorAction SilentlyContinue
if ($null -ne $existing) {
Remove-ItemProperty -Path $state.Path -Name $state.Name -Force
Write-Host "Removed $($state.Path)\$($state.Name) as it did not exist in backup" -ForegroundColor Yellow
}
}
}
}
}
}
function Get-OptimizationSettings {
return @{
"HKLM:\SOFTWARE\Policies\Microsoft\Windows NT\Terminal Services" = @{
"SelectTransport" = @{ Value = 0; Type = "DWord" }
"fEnableVirtualizedGraphics" = @{ Value = 1; Type = "DWord" }
"fEnableRemoteFXAdvancedRemoteApp" = @{ Value = 1; Type = "DWord" }
"MaxCompressionLevel" = @{ Value = 2; Type = "DWord" }
"VisualExperiencePolicy" = @{ Value = 1; Type = "DWord" }
"GraphicsProfile" = @{ Value = 2; Type = "DWord" }
"bEnumerateHWBeforeSW" = @{ Value = 1; Type = "DWord" }
"AVC444ModePreferred" = @{ Value = 1; Type = "DWord" }
"AVCHardwareEncodePreferred" = @{ Value = 1; Type = "DWord" }
"VGOptimization_CaptureFrameRate" = @{ Value = 0x3c; Type = "DWord" }
"VGOptimization_CompressionRatio" = @{ Value = 2; Type = "DWord" }
"ImageQuality" = @{ Value = 3; Type = "DWord" }
}
"HKLM:\SYSTEM\CurrentControlSet\Services\TermDD" = @{
"FlowControlDisable" = @{ Value = 1; Type = "DWord" }
"FlowControlDisplayBandwidth" = @{ Value = 0x10; Type = "DWord" }
"FlowControlChannelBandwidth" = @{ Value = 0x90; Type = "DWord" }
"FlowControlChargePostCompression" = @{ Value = 0; Type = "DWord" }
}
"HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Multimedia\SystemProfile" = @{
"SystemResponsiveness" = @{ Value = 0; Type = "DWord" }
}
"HKLM:\SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations" = @{
"DWMFRAMEINTERVAL" = @{ Value = 0x0f; Type = "DWord" }
}
"HKLM:\SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp" = @{
"InteractiveDelay" = @{ Value = 0; Type = "DWord" }
}
"HKLM:\SYSTEM\CurrentControlSet\Services\LanmanWorkstation\Parameters" = @{
"DisableBandwidthThrottling" = @{ Value = 1; Type = "DWord" }
"DisableLargeMtu" = @{ Value = 0; Type = "DWord" }
}
}
}
function Validate-Optimizations {
$settings = Get-OptimizationSettings
$mismatches = @()
$notFound = @()
$totalSettings = 0
$correctSettings = 0
foreach ($path in $settings.Keys) {
foreach ($name in $settings[$path].Keys) {
$totalSettings++
$expected = $settings[$path][$name]
if (Test-Path $path) {
$property = Get-ItemProperty -Path $path -Name $name -ErrorAction SilentlyContinue
if ($null -ne $property) {
$currentValue = $property.$name
if ($currentValue -eq $expected.Value) {
$correctSettings++
} else {
$mismatches += @{
Path = $path
Name = $name
CurrentValue = $currentValue
ExpectedValue = $expected.Value
Type = $expected.Type
}
}
} else {
$notFound += @{
Path = $path
Name = $name
ExpectedValue = $expected.Value
Type = $expected.Type
}
}
} else {
$notFound += @{
Path = $path
Name = $name
ExpectedValue = $expected.Value
Type = $expected.Type
}
}
}
}
if ($mismatches.Count -eq 0 -and $notFound.Count -eq 0) {
Write-Host "All optimizations are correctly applied!" -ForegroundColor Green
return
}
if ($mismatches.Count -gt 0) {
Write-Host "`nMismatched Values:" -ForegroundColor Yellow
foreach ($mismatch in $mismatches) {
Write-Host "`nRegistry Key: $($mismatch.Path)" -ForegroundColor Cyan
Write-Host "Value Name: $($mismatch.Name)"
Write-Host "Current Value: $($mismatch.CurrentValue)"
Write-Host "Expected Value: $($mismatch.ExpectedValue)"
Write-Host "Type: $($mismatch.Type)"
}
}
if ($notFound.Count -gt 0) {
Write-Host "`nMissing Values:" -ForegroundColor Yellow
foreach ($missing in $notFound) {
Write-Host "`nRegistry Key: $($missing.Path)" -ForegroundColor Cyan
Write-Host "Value Name: $($missing.Name)"
Write-Host "Expected Value: $($missing.ExpectedValue)"
Write-Host "Type: $($missing.Type)"
}
}
$percentOptimized = [math]::Round(($correctSettings / $totalSettings) * 100, 1)
Write-Host "`nOptimization Status: $percentOptimized% optimized" -ForegroundColor Cyan
}
function Apply-RDPOptimizations {
$tsPath = "HKLM:\SOFTWARE\Policies\Microsoft\Windows NT\Terminal Services"
New-Item -Path $tsPath -Force | Out-Null
$tsSettings = @{
"SelectTransport" = 0
"fEnableVirtualizedGraphics" = 1
"fEnableRemoteFXAdvancedRemoteApp" = 1
"MaxCompressionLevel" = 2
"VisualExperiencePolicy" = 1
"GraphicsProfile" = 2
"bEnumerateHWBeforeSW" = 1
"AVC444ModePreferred" = 1
"AVCHardwareEncodePreferred" = 1
"VGOptimization_CaptureFrameRate" = 0x3c
"VGOptimization_CompressionRatio" = 2
"ImageQuality" = 3
}
foreach ($setting in $tsSettings.GetEnumerator()) {
Set-ItemProperty -Path $tsPath -Name $setting.Key -Value $setting.Value -Type DWord
}
$termDDPath = "HKLM:\SYSTEM\CurrentControlSet\Services\TermDD"
New-Item -Path $termDDPath -Force | Out-Null
$termDDSettings = @{
"FlowControlDisable" = 1
"FlowControlDisplayBandwidth" = 0x10
"FlowControlChannelBandwidth" = 0x90
"FlowControlChargePostCompression" = 0
}
foreach ($setting in $termDDSettings.GetEnumerator()) {
Set-ItemProperty -Path $termDDPath -Name $setting.Key -Value $setting.Value -Type DWord
}
Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Multimedia\SystemProfile" `
-Name "SystemResponsiveness" -Value 0 -Type DWord
Set-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations" `
-Name "DWMFRAMEINTERVAL" -Value 0x0f -Type DWord
Set-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp" `
-Name "InteractiveDelay" -Value 0 -Type DWord
$lanmanPath = "HKLM:\SYSTEM\CurrentControlSet\Services\LanmanWorkstation\Parameters"
New-Item -Path $lanmanPath -Force | Out-Null
Set-ItemProperty -Path $lanmanPath -Name "DisableBandwidthThrottling" -Value 1 -Type DWord
Set-ItemProperty -Path $lanmanPath -Name "DisableLargeMtu" -Value 0 -Type DWord
}
function Apply-GamingRDPOptimizations {
$tsPath = "HKLM:\SOFTWARE\Policies\Microsoft\Windows NT\Terminal Services"
New-Item -Path $tsPath -Force | Out-Null
$tsSettings = @{
"SelectTransport" = 0 # UDP preferred
"fEnableVirtualizedGraphics" = 1
"fEnableRemoteFXAdvancedRemoteApp" = 1
"MaxCompressionLevel" = 4 # Increased from 2
"VisualExperiencePolicy" = 1
"GraphicsProfile" = 2
"bEnumerateHWBeforeSW" = 1
"AVC444ModePreferred" = 0 # Disabled 4:4:4
"AVCHardwareEncodePreferred" = 1
"VGOptimization_CaptureFrameRate" = 0x3c # 60fps
"VGOptimization_CompressionRatio" = 4 # More aggressive
"ImageQuality" = 2 # Slightly reduced quality
}
foreach ($setting in $tsSettings.GetEnumerator()) {
Set-ItemProperty -Path $tsPath -Name $setting.Key -Value $setting.Value -Type DWord
}
$termDDPath = "HKLM:\SYSTEM\CurrentControlSet\Services\TermDD"
New-Item -Path $termDDPath -Force | Out-Null
$termDDSettings = @{
"FlowControlDisable" = 1
"FlowControlDisplayBandwidth" = 0x20 # Increased
"FlowControlChannelBandwidth" = 0x90
"FlowControlChargePostCompression" = 0
}
foreach ($setting in $termDDSettings.GetEnumerator()) {
Set-ItemProperty -Path $termDDPath -Name $setting.Key -Value $setting.Value -Type DWord
}
Set-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations" `
-Name "DWMFRAMEINTERVAL" -Value 0x08 -Type DWord
Set-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp" `
-Name "InteractiveDelay" -Value 0 -Type DWord
$lanmanPath = "HKLM:\SYSTEM\CurrentControlSet\Services\LanmanWorkstation\Parameters"
New-Item -Path $lanmanPath -Force | Out-Null
Set-ItemProperty -Path $lanmanPath -Name "DisableBandwidthThrottling" -Value 1 -Type DWord
Set-ItemProperty -Path $lanmanPath -Name "DisableLargeMtu" -Value 0 -Type DWord
}
# Main script execution
$ErrorActionPreference = "Stop"
Write-Host "BetterRDP Optimization Script" -ForegroundColor Green
Write-Host "1. Create backup only"
Write-Host "2. Apply default RDP optimizations"
Write-Host "3. Apply gaming RDP optimizations"
Write-Host "4. Restore from backup"
Write-Host "5. Validate optimization status"
Write-Host "6. Exit"
$choice = Read-Host "Please enter your choice (1-6)"
switch ($choice) {
"1" {
Write-Host "Creating backup..." -ForegroundColor Yellow
$backupPath = Backup-RegistrySettings
if (Validate-Backup -BackupFile $backupPath) {
Write-Host "Backup created successfully at: $backupPath" -ForegroundColor Green
}
}
"2" {
Write-Host "Applying default RDP optimizations..." -ForegroundColor Yellow
Apply-RDPOptimizations
Write-Host "Optimizations applied successfully!" -ForegroundColor Green
}
"3" {
Write-Host "Applying gaming RDP optimizations..." -ForegroundColor Yellow
Apply-GamingRDPOptimizations
Write-Host "Gaming optimizations applied successfully!" -ForegroundColor Green
}
"4" {
$backupPath = ".\rdp_settings_backup.json"
if (Test-Path $backupPath) {
Write-Host "Restoring from backup..." -ForegroundColor Yellow
Restore-RegistrySettings -BackupFile $backupPath
Write-Host "Restore completed successfully!" -ForegroundColor Green
} else {
Write-Host "Backup file not found!" -ForegroundColor Red
}
}
"5" {
Write-Host "Validating optimization status..." -ForegroundColor Yellow
Validate-Optimizations
}
"6" {
Write-Host "Exiting script..." -ForegroundColor Yellow
exit
}
default {
Write-Host "Invalid choice. Exiting script..." -ForegroundColor Red
exit
}
}