Automatically Disable or Enable your GPU (or any other device) when your laptop power state changes
Tags: tutorial, automation, powershell, taskscheduler
Published: 2021-03-15 02:37:28

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fuez2y34z1jcenttftkd.png)

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

4. right-click select properties 
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xaqdcghopfezpwehw353.png)
5. head to the Details section and choose Device Instance Path and copy and paste it into the scripts.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4be1p8hgwsc85epwr5t7.png)

6. save both scripts with .ps1 extension (name it whatever you want here I’m naming them disable.ps1 and enable.ps1)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pdjim9j16rhp26dq4dm8.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1yt1d5qthmnwvvenw7vx.png)


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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m0tsee6vr8rwqy0f224w.png)
3. head to windows logs -> System
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/poahemhl0enhtbrq1w2l.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ciq6ngyhdimq3t1d82gd.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5kc32efy2uujavtjhyea.png)


1. Give the task any name you wish (I’m choosing DisableGPU) and press next
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f73y7ymrhqx4cvvt508o.png)
2. Press next again 
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/93xl72wusgoz9e32mja6.png)

3. choose “start a program” and click next
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ntofiw5ltdtxpgny0bjm.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e6mj8ibw2avv9f0f84dp.png)

6. check the “Open the properties Dialogue for this when I click finish” and click finish 
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7nb8mcw7il8ivvkgz6wi.png)
####Now make sure you modify the task properties as Screenshots below:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8hfwpjfmxuetgaawycou.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dmgdxoirhxjisvf1z9qr.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m0xbicif4yv3x6gy44kx.png)





**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.