-
Notifications
You must be signed in to change notification settings - Fork 29
/
dpst-control.ps1
210 lines (174 loc) · 5.69 KB
/
dpst-control.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
##########################################
# Control Intel Display Power Saving Technology (DPST)
#
# TO DO:
# - Error checking/reporting
# - Writing registry value
# - Saving backup file
# - Exit code to indicate success or failure
# - e.g. If enable is requested, and already enabled, return success
# - e.g. If enable is requested, and not enabled, enable it and return success
# - return failure code if write is unsuccessful
#
# References:
# - https://mikebattistablog.wordpress.com/2016/05/27/disable-intel-dpst-on-sp4/
#
##########################################
# Get command-line parameters
Param(
[switch]$Enable,
[switch]$Disable,
[switch]$Debug
)
# Enforce strict mode
Set-StrictMode -Version Latest
# Stop on any error
$ErrorActionPreference = "Stop"
##########################################
### Variables
$usage = @"
Usage (must Run as Administrator):
Get current state of DPST (default if no option is given)
dpst-control.ps1
Exit code:
0: DPST is disabled
1: DPST is enabled
Enable DPST
dpst-control.ps1 -enable
Disable DPST
dpst-control.ps1 -disable
Other Options:
-debug
Enable debug output
"@
# Path to display adapter ClassGuid
$regBase = 'HKLM:\SYSTEM\CurrentControlSet\Control\Class\{4d36e968-e325-11ce-bfc1-08002be10318}'
# Name of registry key holding ftc data
$ftcName = "FeatureTestControl"
##########################################
### Functions
# Check if running as administrator
# Returns True if Admin, False if not
Function RunningAsAdmin() {
$currentPrincipal = New-Object Security.Principal.WindowsPrincipal(
[Security.Principal.WindowsIdentity]::GetCurrent() )
Return $currentPrincipal.IsInRole(
[Security.Principal.WindowsBuiltInRole]::Administrator )
}
# Locate FTC registry key
# Returns:
# If found: Full path to registry key containing FTC
# If not: $false
Function LocateFTC() {
Param(
[parameter(Mandatory=$true)]$regPath
)
ForEach( $key in
$( Get-ChildItem -ErrorAction SilentlyContinue -LiteralPath "${regPath}" |
Where-Object { $_.Name -match '\\\d{4}$' } )
) {
If( $key.GetValue( $ftcName, $null ) -ne $null ) {
Return $key.Name
}
}
Return $false
}
# Backup current value before changing it
Function BackupFTC() {
Param(
[parameter(Mandatory=$true)]$regPath,
[parameter(Mandatory=$true)]$ftcValue
)
$bkFile = "dpst_backup-$(Get-Date -Format FileDateTime).reg"
$r = "Windows Registry Editor Version 5.00`n`n"
$r += "[$($regPath)]`n"
$r += '"FeatureTestControl"=dword:'
$r += "$(([Convert]::ToString( $ftcValue, 16 )).PadLeft(8, '0'))`n`n"
$r | Out-File $bkFile -NoNewLine
}
# Check if DPST is enabled
# Returns: 0: Disabled; 1: Enabled
Function DPSTEnabled() {
Param(
[parameter(Mandatory=$true)]$ftc,
[parameter(Mandatory=$true)]$bitMask
)
$dpstState = $ftc -band $bitMask
Write-Debug( " State (bin): $(([Convert]::ToString( $dpstState, 2 )).PadLeft(32, '0'))" )
If( $dpstState ) { Return 0 }
Else { Return 1 }
}
##########################################
### Main
If( $Debug ) {
$DebugPreference = "Continue" # Enable debug output
}
# This script must run as admin
If( !(RunningAsAdmin) ) {
Write-Error "Must run as Administrator!"
Exit 255
}
# Search registry for FTC entry
$ftcPath = LocateFTC -regPath ${regBase}
If( $ftcPath -eq $false ) {
Write-Error "Cannot locate ${ftcName} in registry"
Exit 1
}
# Indicate position of DPST bit in binary output
Write-Debug( " ---- DPST bit ----v" )
# Get current value from registry
$ftcCur = (Get-Item -LiteralPath "Registry::${ftcPath}").GetValue($ftcName)
Write-Debug( "Current (hex): 0x$(([Convert]::ToString( $ftcCur, 16 )))" )
Write-Debug( "Current (bin): $(([Convert]::ToString( $ftcCur, 2 )).PadLeft(32, '0'))" )
# Generate bitmask to be used for manipulating DPST bit
# Start with 1 which sets the right-most bit to 1,
# Then shift that bit left X number of times
# DPST bit is the 5th bit from the right.
# Shift 4 times since "1" is in the 1st position
$bitMask = 1 -shl 4
Write-Debug( "Bitmask (bin): $(([Convert]::ToString( $bitMask, 2 )).PadLeft(32, '0'))" )
$ftcNew = 0
$opStr = ''
# Enable DPST (to enable, DPST bit needs to be 0)
If( $Enable ) {
$opStr = "enable"
If( ! (DPSTEnabled -ftc $ftcCur -bitMask $bitMask) ) {
Write-Output "DPST is currently disabled"
# Clear DPST bit (enable DPST)
$ftcNew = $ftcCur -band ( -bnot $bitMask )
}
}
# Disable DPST (to disable, DPST bit needs to be 1)
ElseIf( $Disable ) {
$opStr = "disable"
If( (DPSTEnabled -ftc $ftcCur -bitMask $bitMask) ) {
Write-Output "DPST is currently enabled"
# Set DPST bit (disable DPST)
$ftcNew = $ftcCur -bor $bitMask
}
}
# Report on DPST state and exit
Else {
If( (DPSTEnabled -ftc $ftcCur -bitMask $bitMask) ) {
Write-Output "DPST is enabled"
Exit 1
}
Else {
Write-Output "DPST is disabled"
Exit 0
}
}
# Write new value to registry and report results
If( $ftcNew ) {
Write-Debug( " New (hex): 0x$(([Convert]::ToString( $ftcNew, 16 )))" )
Write-Debug( " New (bin): $(([Convert]::ToString( $ftcNew, 2 )).PadLeft(32, '0'))" )
# Backup current value
BackupFTC -regPath $ftcPath -ftcValue $ftcCur
# Write new value to registry
Set-ItemProperty -Path "Registry::${ftcPath}" -Name $ftcName -Value $ftcNew | Out-Null
Write-Output "DPST is now $($opStr)d.`n"
Write-Warning "-> Reboot is required for changes to take effect. <-"
}
Else {
Write-Output "DPST is already $($opStr)d"
}