-
Notifications
You must be signed in to change notification settings - Fork 122
/
GameMode.ps1
355 lines (301 loc) · 11 KB
/
GameMode.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
# Basic example of using powershell to quickly modify Windows running state before and after playing a game
# e.g. Suspend processes, turn off windows defender, close explorer, and adjust timer resolution
# then restore when done playing
#
# 1. Download procsuspendlist.xml, svcstoplist.xml and svcsuspendlist.xml, edit if necessary or make your own. See how to in variable section.
# 2. Change path references in variables section to your proclist.xml and svclist.xml if used
# 3. Uncomment the desired both On and Off states GM-GameMode-On and GM-GameMode-Off
# Note: For every enabled game mode state, the opposite should also be uncommented
# to restore previous operating state
# 4. Open Powershell as Administrator
# 5. Import-Module C:\PATHTO\gamemode.ps1
# 6. Use GM-GameMode-On to enable the desired settings
# 7. Use GM-GameMode-Off to restore previous state
# 8. Customize, rinse and repeat
#
# Author: djdallmann
# https://github.com/djdallmann/GamingPCSetup
# Happy Scripting :D
#####################
# Variables
#####################
# Referenced Variable Name: $ProcessesSuspendList
$ProcessesSuspendList = [System.Collections.ArrayList]::new()
$procsuspendpath = "C:\Users\ddallmann\Desktop\procsuspendlist.xml"
# Referenced Variable Name: $SvcStopList
$SvcStopList = [System.Collections.ArrayList]::new()
$svcstoppath = "C:\Users\ddallmann\Desktop\svcstoplist.xml"
# Referenced Variable Name: $SvcSuspendList
$SvcSuspendList = [System.Collections.ArrayList]::new()
$svcsuspendpath = "C:\Users\ddallmann\Desktop\svcsuspendlist.xml"
# How To: Example of creating your own list.xml
# $ProcessesList = [System.Collections.ArrayList]::new()
# $ProcessesList.Add("steamwebhelper")
# $ProcessesList.Add("armsvc")
# $ProcessesList.Add("nvdisplay.container")
# $ProcessesList | Export-Clixml -Path C:\Users\ddallmann\Desktop\proclist.xml
$global:taskschedpid = ''
#####################
# External DLL Imports
#####################
$MethodDefinition = @'
[DllImport("ntdll.dll", SetLastError=true)]
public static extern void NtSuspendProcess(IntPtr processHandle);
[DllImport("ntdll.dll", SetLastError=true)]
public static extern void NtResumeProcess(IntPtr processHandle);
[DllImport("ntdll.dll", SetLastError=true)]
public static extern NtStatus NtQueryTimerResolution(out uint MinimumResolution, out uint MaximumResolution, out uint ActualResolution);
[DllImport("ntdll.dll", SetLastError=true)]
public static extern int NtSetTimerResolution(int DesiredResolution, bool SetResolution, out int CurrentResolution );
[DllImport("kernel32.dll", SetLastError=true)]
public static extern bool SetProcessWorkingSetSize(IntPtr hProcess, int dwMinimumWorkingSetSize, int dwMaximumWorkingSetSize);
[DllImport("Dwmapi.dll", SetLastError=true)]
public static extern int DwmEnableMMCSS(bool fEnableMMCSS);
'@
$NtStatus = Add-Type -MemberDefinition $MethodDefinition -Name 'NtStatus' -Namespace 'Win32' -PassThru
#####################
# Functions
#####################
function GM-ImportExternalList($MyPath) {
if (Test-Path -Path "$MyPath") {
Write-Host Importing $MyPath
return Import-Clixml -Path "$MyPath"
} else {
Write-Host "External list not found: $MyPath"
}
}
function GM-SuspendProcess-ByName($NamePattern) {
$processes = ''
$processes = Get-Process -Name "$NamePattern"
Foreach ($i in $processes) {
$ret = [Win32.NtStatus]::NtSuspendProcess($i.handle)
}
}
function GM-ResumeProcess-ByName($NamePattern) {
$processes = ''
$processes = Get-Process -Name "$NamePattern"
Foreach ($i in $processes) {
$ret = [Win32.NtStatus]::NtResumeProcess($i.handle)
}
}
function GM-SuspendProcess-ByPid($ProcID) {
$process = Get-Process -Id "$ProcID"
$ret = [Win32.NtStatus]::NtSuspendProcess($process.handle)
}
function GM-ResumeProcess-ByPid($ProcID) {
$process = Get-Process -Id "$ProcID"
$ret = [Win32.NtStatus]::NtResumeProcess($process.handle)
}
function GM-ResumeProcessesInList($ProcNameList) {
if ($ProcNameList.Count -gt 1) {
Foreach ($i in $ProcNameList) {
Write-Host Resuming $i
GM-ResumeProcess-ByName($i)
}
} else {
Write-Host 'Resumed process count zero, no actions taken'
}
}
function GM-SuspendProcessesInList($ProcNameList) {
if ($ProcNameList.Count -gt 1) {
Foreach ($i in $ProcNameList) {
Write-Host Suspending $i
GM-SuspendProcess-ByName($i)
}
} else {
Write-Host 'Suspend process count zero, no actions taken'
}
}
function GM-SuspendServicesInList($ServiceList) {
$tasksched = $false
if ($ServiceList.Count -gt 1) {
if ($ServiceList.Contains("Schedule")) { $tasksched = $true }
Foreach ($i in $ServiceList) {
$procid = (get-wmiobject win32_service | where { $_.name -eq "$i"}).processID
if ($i -eq "Schedule") { $global:taskschedpid = $procid; continue; }
Write-Host Suspending service: $i
GM-SuspendProcess-ByPid($procid)
}
if ($tasksched) {
Write-Host Suspending service: Schedule
GM-SuspendProcess-ByPid($global:taskschedpid)
}
} else {
Write-Host 'Suspend service count zero, no actions taken'
}
}
function GM-ResumeServicesInList($ServiceList) {
if ($global:taskschedpid -ge 0) {
Write-Host Resuming service: Schedule
Write-Host NOTE: Suspending Task Scheduler will delay the resume process
GM-ResumeProcess-ByPid($global:taskschedpid)
}
if ($ServiceList.Count -gt 1) {
Foreach ($i in $ServiceList) {
if ($i -eq "Schedule") { continue }
$procid = (get-wmiobject win32_service | where { $_.name -eq "$i"}).processID
Write-Host Resuming service: $i
GM-ResumeProcess-ByPid($procid)
}
} else {
Write-Host 'Resumed service count zero, no actions taken'
}
}
function GM-StopServicesInList($ServiceList) {
if ($ServiceList.Count -gt 1) {
Foreach ($i in $ServiceList) {
Write-Host Stopping service: $i
Stop-Service -Name "$i"
}
} else {
Write-Host 'Stop service count zero, no actions taken'
}
}
function GM-StartServicesInList($ServiceList) {
if ($ServiceList.Count -gt 1) {
Foreach ($i in $ServiceList) {
Write-Host Starting service: $i
Start-Service -Name "$i"
}
} else {
Write-Host 'Start service count zero, no actions taken'
}
}
function GM-ResumeProcessesNotInList($ProcNameList) {
$curprocesses = Get-Process
Foreach ($i in $curprocesses) {
$proc = $i.ProcessName | Where-Object { $ProcNameList -Match $_ }
If ($proc) {
Write-Host Skipping $i.ProcessName
continue
} Else {
Write-Host Resuming $i.ProcessName
GM-ResumeProcess-ByName($i.ProcessName)
}
}
}
function GM-SuspendProcessesNotInList($ProcNameList) {
$curprocesses = Get-Process
Foreach ($i in $curprocesses) {
$proc = $i.ProcessName | Where-Object { $ProcNameList -Match $_ }
If ($proc) {
#Write-Host Skipping $i.ProcessName
continue
} Else {
Write-Host Suspending $i.ProcessName
GM-SuspendProcess-ByName($i.ProcessName)
}
}
}
function GM-DisableRealtimeWinDefender() {
#Only works with tamper protection off
Set-MpPreference -DisableRealtimeMonitoring $true
Write-Host Disabling Windows Defender Realtime Protection
}
function GM-EnableRealtimeWinDefender() {
#Only works with tamper protection off
Set-MpPreference -DisableRealtimeMonitoring $false
Write-Host Enabling Windows Defender Realtime Protection
}
function GM-EnableIdle() {
Write-Host PowerCFG Enable Idle
C:\Windows\System32\powercfg -setacvalueindex scheme_current sub_processor 5d76a2ca-e8c0-402f-a133-2158492d58ad 0
C:\Windows\System32\powercfg -setactive scheme_current
}
function GM-DisableIdle() {
Write-Host PowerCFG Disable Idle
C:\Windows\System32\powercfg -setacvalueindex scheme_current sub_processor 5d76a2ca-e8c0-402f-a133-2158492d58ad 1
C:\Windows\System32\powercfg -setactive scheme_current
}
function GM-QueryTimerResolution() {
$ntqtrmin = $null
$ntqtrmax = $null
$ntqtrcur = $null
[Win32.NtStatus]::NtQueryTimerResolution([ref]$ntqtrmin, [ref]$ntqtrmax, [ref]$ntqtrcur)
Write-Host "Current Timer Res: $ntqtrcur `r`nTimer Res Minimum: $ntqtrmin `r`nTimer Res Maximum: $ntqtrmax"
}
function GM-SetTimerResolution($timerres) {
if ($timerres -ge 4000) {
#Set to 0.5ms e.g. 5000
$ntdesiredres = $timerres
$ntsetres = $true
$ntcurrentres = 156250
Write-Host Setting timer resolution to $timerres
$ret1 = [Win32.NtStatus]::NtSetTimerResolution($ntdesiredres,$ntsetres,[ref]$ntcurrentres)
GM-QueryTimerResolution
}
}
function GM-ReleaseTimerResolution() {
#Default 15ms
$ntdesiredres = 156250
$ntsetres = $true
$ntcurrentres = 156250
Write-Host Resetting timer resolution
$ret1 = [Win32.NtStatus]::NtSetTimerResolution($ntdesiredres,$ntsetres,[ref]$ntcurrentres)
}
function GM-KillExplorer() {
Set-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name AutoRestartShell -Value 0
Stop-Process -Name "explorer" -Force
}
function GM-ReviveExplorer() {
Set-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name AutoRestartShell -Value 1
C:\Windows\explorer.exe
}
function GM-TrimWorkingSetAll() {
$processes = Get-Process
Foreach ($i in $processes) {
if ($processes.handle) {
$ret = [Win32.NtStatus]::SetProcessWorkingSetSize($processes[0].handle, -1, -1)
}
}
}
function GM-DwmEnableMMCSS([bool]$option) {
#TRUE to instruct DWM to participate in MMCSS scheduling; FALSE to opt out or end participation in MMCSS scheduling.
if ($option) {
Write-Host Requesting that DWM participate in MMCSS scheduling
$ret = [Win32.NtStatus]::DwmEnableMMCSS($option)
} else {
Write-Host "Requesting that DWM 'NOT' participate in MMCSS scheduling"
$ret = [Win32.NtStatus]::DwmEnableMMCSS($option)
}
if ($ret -gt 0) {
Write-Host DwmEnableMMCSS Enable Failed: $ret
}
#https://docs.microsoft.com/en-us/windows/win32/seccrypto/common-hresult-values
#https://docs.microsoft.com/en-us/windows/win32/api/dwmapi/nf-dwmapi-dwmenablemmcss
}
function GM-SetWin32PrioritySeparation($decvalue) {
Set-ItemProperty "HKLM:\SYSTEM\CurrentControlSet\Control\PriorityControl" -Name Win32PrioritySeparation -Value $decvalue
#https://docs.microsoft.com/en-us/previous-versions//cc976120(v=technet.10)?redirectedfrom=MSDN
}
function GM-RestoreWin32PrioritySeparationProgramDefault() {
Set-ItemProperty "HKLM:\SYSTEM\CurrentControlSet\Control\PriorityControl" -Name Win32PrioritySeparation -Value 2
#https://docs.microsoft.com/en-us/previous-versions//cc976120(v=technet.10)?redirectedfrom=MSDN
}
function GM-GameMode-On() {
#GM-SetTimerResolution(5000)
GM-KillExplorer
#GM-DisableIdle
GM-DisableRealtimeWinDefender
GM-SuspendProcessesInList($ProcessesSuspendList)
GM-StopServicesInList($SvcStopList)
GM-SuspendServicesInList($SvcSuspendList)
#GM-SetWin32PrioritySeparation(2)
GM-TrimWorkingSetAll
}
function GM-GameMode-Off() {
#GM-ReleaseTimerResolution
GM-ReviveExplorer
#GM-EnableIdle
GM-EnableRealtimeWinDefender
GM-ResumeProcessesInList($ProcessesSuspendList)
GM-StartServicesInList($SvcStopList)
GM-ResumeServicesInList($SvcSuspendList)
GM-RestoreWin32PrioritySeparationProgramDefault
}
#####################
# Imported Lists
#####################
$ProcessesSuspendList = GM-ImportExternalList($procsuspendpath)
$SvcStopList = GM-ImportExternalList($svcstoppath)
$SvcSuspendList = GM-ImportExternalList($svcsuspendpath)