-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBatteryRefreshLaptopMonitor.ps1
57 lines (48 loc) · 1.64 KB
/
BatteryRefreshLaptopMonitor.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
param (
[int]$HzOnBattery,
[int]$HzOnPlug
)
function Set-RefreshRate {
param (
[int]$Rate = 60
)
# Check if NirCmd is present
if (-Not (Test-Path "./nircmd.exe")) {
Write-Host "NirCmd not found. Ensure it is in the script directory or in PATH."
return
}
# Set the refresh rate for the primary monitor using NirCmd
$resolution = Get-WmiObject -Class Win32_DesktopMonitor | Select-Object ScreenWidth, ScreenHeight
$monitor = Get-WmiObject -Class Win32_DisplayControllerConfiguration
$bits = $monitor.BitsPerPixel
$process = Start-Process -FilePath "./nircmd.exe" -ArgumentList "setdisplay $($resolution.ScreenWidth) $($resolution.ScreenHeight) $bits $Rate" -NoNewWindow -Wait
if ($process.ExitCode -eq 0) {
Write-Host "Refresh rate set to $Rate Hz."
}
else {
Write-Host "Failed to set refresh rate to $Rate Hz. Error Code: $($process.ExitCode)"
}
}
function Get-PluggedIn {
$battery = Get-WmiObject Win32_Battery
if ($battery.BatteryStatus -eq 2) {
return $true
}
return $false
}
# Main script logic
Write-Host "Listening for power state changes. Press Ctrl+C to exit."
$last_pluggedIn = Get-PluggedIn
while ($true) {
$pluggedIn = Get-PluggedIn
# Determine refresh rate based on power state
$refreshRate = if ($pluggedIn) { $HzOnPlug } else { $HzOnBattery }
if (-not $refreshRate) {
Write-Host "Parameters -HzOnPlug and -HzOnBattery are required and must be non zero."
break
}
if ($last_pluggedIn -ne $pluggedIn) {
Set-RefreshRate -Rate $refreshRate
}
$last_pluggedIn = $pluggedIn
}