Why is Task Scheduler opening Note Block instead of running my script?

Riccardo Chimisso 1 Reputation point
2022-05-28T09:59:28.25+00:00

I'm on Windows 10 and I want to schedule a task to change which GPU my browser should use depending whether I am on battery or ac.
I managed to create a task as follows (I changed personal info with an all uppercase string of what they represent):
<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.4" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<RegistrationInfo>
<Date>2022-05-28T10:52:12.1836624</Date>
<Author>AUTHOR</Author>
<URI>\Change Opera GX GPU Setting</URI>
</RegistrationInfo>
<Triggers>
<EventTrigger>
<Enabled>true</Enabled>
<Subscription><QueryList><Query Id="0" Path="Microsoft-Windows-Kernel-Power/Thermal-Operational"><Select Path="Microsoft-Windows-Kernel-Power/Thermal-Operational">*[System[Provider[@DeezNutz ='Microsoft-Windows-Kernel-Power'] and EventID=105]]</Select></Query></QueryList></Subscription>
</EventTrigger>
</Triggers>
<Principals>
<Principal id="Author">
<UserId>USERID</UserId>
<LogonType>Password</LogonType>
<RunLevel>HighestAvailable</RunLevel>
</Principal>
</Principals>
<Settings>
<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
<DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>false</StopIfGoingOnBatteries>
<AllowHardTerminate>true</AllowHardTerminate>
<StartWhenAvailable>true</StartWhenAvailable>
<RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
<IdleSettings>
<StopOnIdleEnd>true</StopOnIdleEnd>
<RestartOnIdle>false</RestartOnIdle>
</IdleSettings>
<AllowStartOnDemand>true</AllowStartOnDemand>
<Enabled>true</Enabled>
<Hidden>false</Hidden>
<RunOnlyIfIdle>false</RunOnlyIfIdle>
<DisallowStartOnRemoteAppSession>false</DisallowStartOnRemoteAppSession>
<UseUnifiedSchedulingEngine>true</UseUnifiedSchedulingEngine>
<WakeToRun>false</WakeToRun>
<ExecutionTimeLimit>PT0S</ExecutionTimeLimit>
<Priority>7</Priority>
<RestartOnFailure>
<Interval>PT1M</Interval>
<Count>3</Count>
</RestartOnFailure>
</Settings>
<Actions Context="Author">
<Exec>
<Command>powershell</Command>
<Arguments>-ExecutionPolicy Bypass -File PATHTOSCRIPT\opera-gpu-setting-update.ps1</Arguments>
</Exec>
</Actions>
</Task>
And it works fine if I manually run the task. However if I plug my laptop it just opens the script file with Windows Notepad instead of running the script. Furthermore if I unplug my laptop the task doesn't seem to run at all (no script is being executed and Notepad does not open).

Why is this happening? How do I fix this?

Windows 10
Windows 10
A Microsoft operating system that runs on personal computers and tablets.
10,570 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,355 questions
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. ELBANNOUNI.MOHAMMED 81 Reputation points
    2022-05-28T11:35:47.71+00:00

    Le planificateur de tâches Microsoft Windows vous permet de lancer automatiquement un programme ou un script PowerShell à un moment précis ou lorsque certaines conditions sont satisfaites. Il permet également de programmer l’envoi d’e-mails et même l’affichage de certains messages. Dans ce billet de blog, nous allons vous montrer comment exécuter un script PowerShell à partir du planificateur de tâches. Ce script vous alertera de toute installation de logiciel sur un ordinateur local.

    0 comments No comments

  2. Riccardo Chimisso 1 Reputation point
    2022-05-28T11:59:54.553+00:00

    In the end Notepad opening was because I had created another task by mistake.
    Anyway, I fixed the real issue by changing the Subscription as follows:
    <Subscription>&lt;QueryList&gt;&lt;Query Id="0" Path="System"&gt;&lt;Select Path="System"&gt;*[System[Provider[@Name='Microsoft-Windows-Kernel-Power'] and EventID=105]]&lt;/Select&gt;&lt;/Query&gt;&lt;/QueryList&gt;</Subscription>

    0 comments No comments

  3. MotoX80 31,561 Reputation points
    2022-05-28T12:23:36.83+00:00

    You will need 2 tasks, correct? One when you go from battery to AC (set gpu to full) and the other for AC to battery (set gpu to low power). Or does your script look at the event to which power mode is set?

    Notepad is the default association for .ps1 files. So if you see notepad running then somewhere you are trying to run a .ps1 file without specifying the executable "powershell.exe C:\Myscript.ps1".

    In your tasks verify that the program is "powershell.exe" or maybe even the full path; "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe".

    In the task scheduler's action pane, enable all tasks history so that you can see when it was run.

    You might also want to add a start and stop-transcript to your script to get a full log of what Powershell did.

    https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.host/start-transcript?view=powershell-5.1

    https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.host/stop-transcript?view=powershell-5.1

    0 comments No comments

  4. Limitless Technology 39,336 Reputation points
    2022-06-06T07:22:55.623+00:00

    Hi there,

    If the task scheduler works only when you plug the Laptop try changing the Power Conditions for the task and see if that helps. To do this, refer the steps mentioned below:

    -Press Windows logo key + R, to open Run dialog box.
    -Type taskschd.msc and click on OK.
    -Click on Task Scheduler Library.
    -Double click on the task that you have created, navigate to the Conditions tab.
    -Under Power, clear the checkbox for Stop if the computer switches to battery power.


    --If the reply is helpful, please Upvote and Accept it as an answer--

    0 comments No comments