Skip to content

TingRongYou/AutoPowerSwitcher

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 

Repository files navigation

Auto Power Switcher Daemon

A lightweight, zero-overhead Windows background daemon that automatically manages power plans, screen refresh rates, and display brightness based on physical AC power connection, suitable especially for power-exhaustive gaming laptops.

Unlike bloated third-party software, this script runs entirely native to Windows, utilizing a suspended PowerShell thread to keep CPU usage at exactly 0.00% while running.

✨ Features

  • Instant Power State Switching: Automatically toggles between Windows 'High Performance' and 'Power Saver' plans.
  • Dynamic Refresh Rate Injection: Uses in-memory compiled C# to step the primary laptop display down to 60Hz on battery, and up to 165Hz when plugged in.
  • Multi-Monitor Safe: Automatically detects and ignores external monitors (e.g., 180Hz displays) to prevent accidental refresh rate overrides.
  • WMI Brightness Control: Dims the screen to 20% on battery and restores it to 60% on AC power.
  • Zero Overhead: Operates via an infinite loop that suspends the thread entirely between checks.

🛠️ Techniques & Technologies Used

  • PowerShell: Serves as the core logic wrapper and background loop daemon.
  • C# / .NET On-the-Fly Compilation: Uses Add-Type to compile raw C# code directly in memory at runtime.
  • Windows API (P/Invoke): Hooks directly into user32.dll to utilize EnumDisplaySettings and ChangeDisplaySettingsEx for hardware-level screen manipulation.
  • WMI (Windows Management Instrumentation): Interacts with the motherboard hardware to control backlight brightness natively.

⚠️ Compatibility

  • Compatibility Note: This daemon uses native Windows APIs and works across various laptop brands (Acer, ASUS, Lenovo, Dell, etc.). However, if your laptop includes proprietary control software (like ASUS Armoury Crate, Lenovo Vantage, or Acer NitroSense), you must disable their built-in "Panel Overdrive" or "Auto Refresh Rate" features. If both your manufacturer software and this script attempt to change the refresh rate simultaneously, it may cause screen flickering.

⚙️ Configuration (For Other Users)

This script is currently optimized for a 165Hz primary display and a 180Hz external monitor. If you are cloning this repository, open AutoPowerSwitcher.ps1 and modify the following values to match your hardware:

  1. Safety Catch (External Monitors): Change 180 to the refresh rate of your external monitor. if (dm.dmDisplayFrequency == 180) { continue; }
  2. High Performance Refresh Rate: Change 165 to your laptop's max refresh rate. [ScreenMonitor]::SetRefreshRate(165)
  3. Power Saver Refresh Rate: Standard is 60Hz, but can be modified here: [ScreenMonitor]::SetRefreshRate(60)

🚀 Installation & Setup

  1. Create the Directory

    • Create a dedicated folder at C:\Scripts\
    • Place AutoPowerSwitcher.ps1 inside this folder.
    • Security Note: Ensure standard Windows 'Users' only have "Read & Execute" permissions on this folder to prevent file tampering.
  2. Configure Windows Task Scheduler

    • Open Task Scheduler and click Create Task... (Not Basic Task).
    • General Tab:
      • Name: Auto Power Daemon
      • CRITICAL: Select the radio button Run only when user is logged on. (If run in the background without a logged-in user, Windows cannot detect the display to change the refresh rate).
      • Check: Run with highest privileges (Required for Powercfg & WMI).
      • Check: Hidden
    • Triggers Tab:
      • New Trigger -> Begin the task: At log on.
    • Actions Tab:
      • Action: Start a program
      • Program/script: powershell.exe
      • Add arguments: -WindowStyle Hidden -ExecutionPolicy Bypass -File "C:\Scripts\AutoPowerSwitcher.ps1"
    • Conditions Tab:
      • CRITICAL: Uncheck "Start the task only if the computer is on AC power".
  3. Configure Sleep Settings To prevent the laptop from sleeping while plugged in on the High Performance plan, open an Administrator PowerShell and run:

    powercfg /change monitor-timeout-ac 0
    powercfg /change standby-timeout-ac 0

🗺️ Roadmap & Future Improvements

This daemon is actively being refined. Planned future features include:

  • Custom Fan Curve Integration: Researching methods to interface with OEM Embedded Controllers (EC) via ACPI calls to dynamically adjust fan speeds without relying on proprietary bloatware (e.g., Acer NitroSense).
  • Config File Extraction: Moving the hardcoded variables (Refresh Rates, GUIDs, Brightness percentages) out of the .ps1 script and into a standalone .json or .ini configuration file for easier user customization.
  • Installer Script: Developing a secondary script to automatically configure the Windows Task Scheduler environment and folder permissions for non-technical users.

📝 License

This project is open-source and free to use.

About

A lightweight, zero-overhead Windows background daemon that automatically lowers the refresh rate, dims the brightness, and switches to power saving mode on battery, restoring high performance when plugged in.

Topics

Resources

Stars

Watchers

Forks

Contributors