DEV Community

Cover image for Automatically Disable or Enable your GPU (or any other device) when your laptop power state changes
Muhammad
Muhammad

Posted on

Automatically Disable or Enable your GPU (or any other device) when your laptop power state changes

I purchased a gaming laptop nearly eighteen months ago and I was so happy and hyped about it until it started to overheat and give me bluescreens of death. I searched a lot on google and Acer’s community for a fix, but unfortunately, there was none. After so many attempts to fix it, I figured out that the discrete Graphics Card (Nvidia 1660Ti) was causing the problem whenever the laptop was running on Battery. Therefore, I decided to disable the GPU whenever I was using the laptop on battery, but it was taking a lot of my time (probably 15 seconds every time) and if you know me you’ll probably already know that I don’t love wasting time(😁). So I figured out one way to disable it automatically whenever I remove the charger and enable it otherwise.

What do you need to do?

1. Write two simple PowerShell scripts to disable and enable your GPU(or any other device)
2. Find the event logged when you plug and unplug your power adapter using Windows Event Viewer.
3. Attach a task to each Event using TaskScheduler

Step One - Writing the Script

No need to worry, the script is already there all you have to do is replace the InstanceID with your GPU’s(or any other devices) Instance ID without removing the quotations.

Script for Disabling :

pnputil /disable-device "InstanceID"

Script for Enabling:

pnputil /enable-device "InstanceID"

How to get the Devices Instance ID? Easy just follow the steps:

  1. press Windows Key + R to open Run.

  2. paste devmgmt.msc to open device manager.
    Alt Text

  3. select the device you want (for me it’s Nvidia Geforce GTX 1660Ti)

  4. right-click select properties
    Alt Text

  5. head to the Details section and choose Device Instance Path and copy and paste it into the scripts.
    Alt Text

  6. save both scripts with .ps1 extension (name it whatever you want here I’m naming them disable.ps1 and enable.ps1)
    Alt Text
    Alt Text

Now your Scripts are ready. Next, we are going to Log the power plug and unplug events.

Step Two - Logging the event

Now, we need to know which event is being logged when we plug and unplug the charger. For that, we have to head to the Windows event viewer. Follow me:

  1. open Run again, press Windows Key + R
  2. type eventvwr.msc and press enter Alt Text
  3. head to windows logs -> System Alt Text
  4. now plug and unplug your charger multiple times (two to three is enough) to see the event that’s being logged.
  5. refresh the list by pressing F5 and you’ll see multiple events with the “Kernel-Power” source.

  6. select one of them and click on the Details section below
    Alt Text

  7. there you’ll see something like

AcOnline true

Or

AcOnline false

The first one is for the event that’s raised when you plug in the power and the other one is for unplugging the power (if yours is any of the two don’t worry the other one is the event below or above it).

Now we have found the event, the next step is to attach a task to each of the events. Here, I'm only going to attach a task to the unplugging event, but you’ll need to do it for the plugging too. Don’t panic, it’s the same step except that you have to attach the enable script to the plugging task instead of the disable script.

Step Three - Attaching a Task to the Events

After finding the events simply right-click on one of the events ( in my case it’s the unplug event) and choose “Attach Task to This Event” and follow the upcoming steps:
Alt Text

  1. Give the task any name you wish (I’m choosing DisableGPU) and press next Alt Text
  2. Press next again
    Alt Text

  3. choose “start a program” and click next
    Alt Text

  4. Type Powershell.exe in the “Program/Script” filed

  5. type -ExecutionPolicy Bypass -FILE "E:\disable.ps1" in the “Add arguments” field with
    E:\disable.ps1 replaced by your path to the disable script and press click next.
    Alt Text

  6. check the “Open the properties Dialogue for this when I click finish” and click finish
    Alt Text

    Now make sure you modify the task properties as Screenshots below:

    Alt Text
    Alt Text
    Alt Text

Hooray 🎉🎉🎉, now your laptop will automatically disable the GPU whenever you remove the power cord but don’t forget you need to automatically turn it back on by attaching the plug event to a task that runs enable GPU script.

Top comments (8)

Collapse
 
graal432 profile image
graal432 • Edited

The game will be challenging to play with your settings. It is, however, dependant on the game. It's possible that the game isn't working correctly on your video graphics card. Anyway, have you thought of getting an Nvidia GTX? Yes, I am aware that prices are at an all-time low right now. There are, however, some excellent models available at a reasonable price. Yesterday, I spotted the GTX 1660, which is the most popular because of its power for the price. There are various models for 1660. If you're considering buying a 1660 Ti or a 1660 Super, see this post quickreviewshow.com/1660-ti-vs-166... I sincerely hope I was able to aid someone!

Collapse
 
nutrionik profile image
Nutrionik

That's a great suggestion for graphic cards thanks for sharing!

Collapse
 
krunk profile image
B1adedriver • Edited

Hello, strangely this doesn't work 100% of the time for me. With device manager open, disconnecting and reconnecting the power cable a few times, the dGPU turns off and on correctly, but every couple of times I connect it to power it says I can disable the dGPU in the top bar, despite it having the disabled icon on the dGPU.

Collapse
 
pedrovenancio profile image
Pedro Venancio

Hi @muhammedziyad
Thank you very much for this great tip! It works really well when plugging and unplugging the charger. But it does not work on windows startup because the event is not fired.
Do you now any workaround to check the status of kernel power source on windows startup and if AcOnline is false, run the script to deactivate GPU, and if AcOnline is true, run the script to activate GPU?
Thanks @muhammedziyad !
Best regards!

Collapse
 
barmalini18 profile image
barmalini18 • Edited

You can use custom xml filter for task scheduler, where
Data='true' means laptop running on Ac
Data='false' running on battery

<QueryList>
  <Query Id="0" Path="System">
    <Select Path="System">*[System[(EventID=105)]] and *[System[Provider[@Name="Microsoft-Windows-Kernel-Power"]]] and *[EventData[Data[@Name="AcOnline"] and (Data='true')]]</Select>
  </Query>
</QueryList>
Enter fullscreen mode Exit fullscreen mode
Collapse
 
mhmd_azeez profile image
Muhammad Azeez

Well done, a very useful article

Collapse
 
tolache profile image
Anatoly Cherenkov

Thanks!

Collapse
 
__9c61eb0428643107b662 profile image
呈義 張

Is there any method to hide the powershell window while running?