2

I'm trying to make a batch file that creates an xml file then imports that file into scheduled tasks, plus a few other things. I know how to import the xml and do everything else I need, but I can't get the creation of the xml file to work right.

Here's the content of the xml file exported from scheduled tasks:

<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.4" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
  <RegistrationInfo>
    <Date>2021-01-11T04:56:22.2785148</Date>
    <Author>Computer_Name\User</Author>
    <URI>\Display Off When Locked (Battery Only)</URI>
  </RegistrationInfo>
  <Triggers>
    <SessionStateChangeTrigger>
      <Enabled>true</Enabled>
      <StateChange>SessionLock</StateChange>
    </SessionStateChangeTrigger>
  </Triggers>
  <Principals>
    <Principal id="Author">
      <GroupId>S-1-5-32-544</GroupId>
      <RunLevel>HighestAvailable</RunLevel>
    </Principal>
  </Principals>
  <Settings>
    <MultipleInstancesPolicy>StopExisting</MultipleInstancesPolicy>
    <DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
    <StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
    <AllowHardTerminate>true</AllowHardTerminate>
    <StartWhenAvailable>false</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>PT1H</ExecutionTimeLimit>
    <Priority>7</Priority>
  </Settings>
  <Actions Context="Author">
    <Exec>
      <Command>"C:\Portable Apps\MonOff (Display off When Locked)\MonOff (Battery Only).bat"</Command>
    </Exec>
  </Actions>
</Task>

And here's the contents of a batch file whose sole purpose is to create an identical xml file:

setlocal enableDelayedExpansion

echo ^<?xml version="1.0" encoding="UTF-16"?^>> task.xml
echo ^<Task version="1.4" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task"^>>> task.xml
echo   ^<RegistrationInfo^>>> task.xml
echo     ^<Date^>2021-01-11T04:56:22.2785148^</Date^>>> task.xml
echo     ^<Author^>Computer_Name\User^</Author^>>> task.xml
echo     ^<URI^>\Display Off When Locked (Battery Only)^</URI^>>> task.xml
echo   ^</RegistrationInfo^>>> task.xml
echo   ^<Triggers^>>> task.xml
echo     ^<SessionStateChangeTrigger^>>> task.xml
echo       ^<Enabled^>true^</Enabled^>>> task.xml
echo       ^<StateChange^>SessionLock^</StateChange^>>> task.xml
echo     ^</SessionStateChangeTrigger^>>> task.xml
echo   ^</Triggers^>>> task.xml
echo   ^<Principals^>>> task.xml
echo     ^<Principal id="Author"^>>> task.xml
echo       ^<GroupId^>S-1-5-32-544^</GroupId^>>> task.xml
echo       ^<RunLevel^>HighestAvailable^</RunLevel^>>> task.xml
echo     ^</Principal^>>> task.xml
echo   ^</Principals^>>> task.xml
echo   ^<Settings^>>> task.xml
echo     ^<MultipleInstancesPolicy^>StopExisting^</MultipleInstancesPolicy^>>> task.xml
echo     ^<DisallowStartIfOnBatteries^>false^</DisallowStartIfOnBatteries^>>> task.xml
echo     ^<StopIfGoingOnBatteries^>true^</StopIfGoingOnBatteries^>>> task.xml
echo     ^<AllowHardTerminate^>true^</AllowHardTerminate^>>> task.xml
echo     ^<StartWhenAvailable^>false^</StartWhenAvailable^>>> task.xml
echo     ^<RunOnlyIfNetworkAvailable^>false^</RunOnlyIfNetworkAvailable^>>> task.xml
echo     ^<IdleSettings^>>> task.xml
echo       ^<StopOnIdleEnd^>true^</StopOnIdleEnd^>>> task.xml
echo       ^<RestartOnIdle^>false^</RestartOnIdle^>>> task.xml
echo     ^</IdleSettings^>>> task.xml
echo     ^<AllowStartOnDemand^>true^</AllowStartOnDemand^>>> task.xml
echo     ^<Enabled^>true^</Enabled^>>> task.xml
echo     ^<Hidden^>false^</Hidden^>>> task.xml
echo     ^<RunOnlyIfIdle^>false^</RunOnlyIfIdle^>>> task.xml
echo     ^<DisallowStartOnRemoteAppSession^>false^</DisallowStartOnRemoteAppSession^>>> task.xml
echo     ^<UseUnifiedSchedulingEngine^>true^</UseUnifiedSchedulingEngine^>>> task.xml
echo     ^<WakeToRun^>false^</WakeToRun^>>> task.xml
echo     ^<ExecutionTimeLimit^>PT1H^</ExecutionTimeLimit^>>> task.xml
echo     ^<Priority^>7^</Priority^>>> task.xml
echo   ^</Settings^>>> task.xml
echo   ^<Actions Context="Author"^>>> task.xml
echo     ^<Exec^>>> task.xml
echo       ^<Command^>"C:\Portable Apps\MonOff (Display off When Locked)\MonOff (Battery Only).bat"^</Command^>>> task.xml
echo     ^</Exec^>>> task.xml
echo   ^</Actions^>>> task.xml
echo ^</Task^>>> task.xml

endlocal & exit /b

When I view both of them in a text editor, they're identical. But task.xml is only 2KB vs 4KB for the original, and viewing them in WinMerge shows the content in the original but task.xml just shows a bunch of Asian-looking characters on one long line:

㼼浸敶獲潩㵮ㄢ〮•湥潣楤杮∽呕ⵆ㘱㼢ാ㰊慔歳瘠牥楳湯∽⸱∴砠汭獮∽瑨灴⼺猯档浥獡洮捩潲潳瑦挮浯眯湩潤獷㈯〰⼴㈰洯瑩琯獡≫ാ 㰠敒楧瑳慲楴湯湉潦ാ †㰠慄整㈾㈰ⴱ㄰ㄭ吱㐰㔺㨶㈲㈮㠷ㄵ㠴⼼慄整ാ †㰠畁桴牯䔾协卜整敶⼼畁桴牯ാ †㰠剕㹉䑜獩汰祡传晦圠敨潌正摥⠠慂瑴牥⁹湏祬㰩唯䥒ാ 㰠刯来獩牴瑡潩䥮普㹯਍†吼楲杧牥㹳਍††匼獥楳湯瑓瑡䍥慨杮呥楲杧牥ാ ††㰠湅扡敬㹤牴敵⼼湅扡敬㹤਍†††匼慴整桃湡敧匾獥楳湯潌正⼼瑓瑡䍥慨杮㹥਍††⼼敓獳潩卮慴整桃湡敧牔杩敧㹲਍†⼼牔杩敧獲ാ 㰠牐湩楣慰獬ാ †㰠牐湩楣慰摩∽畁桴牯㸢਍†††䜼潲灵摉匾ㄭ㔭㌭ⴲ㐵㰴䜯潲灵摉ാ ††㰠畒䱮癥汥䠾杩敨瑳癁楡慬汢㱥刯湵敌敶㹬਍††⼼牐湩楣慰㹬਍†⼼牐湩楣慰獬ാ 㰠敓瑴湩獧ാ †㰠畍瑬灩敬湉瑳湡散偳汯捩㹹瑓灯硅獩楴杮⼼畍瑬灩敬湉瑳湡散偳汯捩㹹਍††䐼獩污潬卷慴瑲晉湏慂瑴牥敩㹳慦獬㱥䐯獩污潬卷慴瑲晉湏慂瑴牥敩㹳਍††匼潴䥰䝦楯杮湏慂瑴牥敩㹳牴敵⼼瑓灯晉潇湩佧䉮瑡整楲獥ാ †㰠汁潬䡷牡呤牥業慮整琾畲㱥䄯汬睯慈摲敔浲湩瑡㹥਍††匼慴瑲桗湥癁楡慬汢㹥慦獬㱥匯慴瑲桗湥癁楡慬汢㹥਍††刼湵湏祬晉敎睴牯䅫慶汩扡敬显污敳⼼畒佮汮䥹书瑥潷歲癁楡慬汢㹥਍††䤼汤卥瑥楴杮㹳਍†††匼潴佰䥮汤䕥摮琾畲㱥匯潴佰䥮汤䕥摮ാ ††㰠敒瑳牡佴䥮汤㹥慦獬㱥刯獥慴瑲湏摉敬ാ †㰠䤯汤卥瑥楴杮㹳਍††䄼汬睯瑓牡佴䑮浥湡㹤牴敵⼼汁潬卷慴瑲湏敄慭摮ാ †㰠湅扡敬㹤牴敵⼼湅扡敬㹤਍††䠼摩敤㹮慦獬㱥䠯摩敤㹮਍††刼湵湏祬晉摉敬显污敳⼼畒佮汮䥹䥦汤㹥਍††䐼獩污潬卷慴瑲湏敒潭整灁印獥楳湯显污敳⼼楄慳汬睯瑓牡佴剮浥瑯䅥灰敓獳潩㹮਍††唼敳湕晩敩卤档摥汵湩䕧杮湩㹥牴敵⼼獕啥楮楦摥捓敨畤楬杮湅楧敮ാ †㰠慗敫潔畒㹮慦獬㱥圯歡呥副湵ാ †㰠硅捥瑵潩呮浩䱥浩瑩倾ㅔ㱈䔯數畣楴湯楔敭楌業㹴਍††值楲牯瑩㹹㰷倯楲牯瑩㹹਍†⼼敓瑴湩獧ാ 㰠捁楴湯⁳潃瑮硥㵴䄢瑵潨≲ാ †㰠硅捥ാ ††㰠潃浭湡㹤䌢尺潐瑲扡敬䄠灰屳潍佮晦⠠楄灳慬⁹景⁦桗湥䰠捯敫⥤䵜湯晏⁦䈨瑡整祲传汮⥹戮瑡㰢䌯浯慭摮ാ †㰠䔯數㹣਍†⼼捁楴湯㹳਍⼼慔歳ാ

And, of course, this newly created xml doesn't import into task scheduler, which presents the following error:

The format of the task is not valid. The following error was reported: (1,2)::ERROR: incorrect document syntax

Also, HxD shows very different content between the two files:

Original:

ÿþ<�?�x�m�l� �v�e�r�s�i�o�n�=�"�1�.�0�"� �e�n�c�o�d�i�n�g�=�"�U�T�F�-�1�6�"�?�>�
�
�<�T�a�s�k� �v�e�r�s�i�o�n�=�"�1�.�4�"� �x�m�l�n�s�=�"�h�t�t�p�:�/�/�s�c�h�e�m�a�s�.�m�i�c�r�o�s�o�f�t�.�c�o�m�/�w�i�n�d�o�w�s�/�2�0�0�4�/�0�2�/�m�i�t�/�t�a�s�k�"�>�
�
� � �<�R�e�g�i�s�t�r�a�t�i�o�n�I�n�f�o�>�
�
� � � � �<�D�a�t�e�>�2�0�2�1�-�0�1�-�1�1�T�0�4�:�5�6�:�2�2�.�2�7�8�5�1�4�8�<�/�D�a�t�e�>�
�
� � � � �<�A�u�t�h�o�r�>�C�o�m�p�u�t�e�r�_�N�a�m�e�\�U�s�e�r�<�/�A�u�t�h�o�r�>�
�
� � � � �<�U�R�I�>�\�D�i�s�p�l�a�y� �O�f�f� �W�h�e�n� �L�o�c�k�e�d� �(�B�a�t�t�e�r�y� �O�n�l�y�)�<�/�U�R�I�>�
�
� � �<�/�R�e�g�i�s�t�r�a�t�i�o�n�I�n�f�o�>�
�
� � �<�T�r�i�g�g�e�r�s�>�
�
� � � � �<�S�e�s�s�i�o�n�S�t�a�t�e�C�h�a�n�g�e�T�r�i�g�g�e�r�>�
�
� � � � � � �<�E�n�a�b�l�e�d�>�t�r�u�e�<�/�E�n�a�b�l�e�d�>�
�
� � � � � � �<�S�t�a�t�e�C�h�a�n�g�e�>�S�e�s�s�i�o�n�L�o�c�k�<�/�S�t�a�t�e�C�h�a�n�g�e�>�
�
� � � � �<�/�S�e�s�s�i�o�n�S�t�a�t�e�C�h�a�n�g�e�T�r�i�g�g�e�r�>�
�
� � �<�/�T�r�i�g�g�e�r�s�>�
�
� � �<�P�r�i�n�c�i�p�a�l�s�>�
�
� � � � �<�P�r�i�n�c�i�p�a�l� �i�d�=�"�A�u�t�h�o�r�"�>�
�
� � � � � � �<�G�r�o�u�p�I�d�>�S�-�1�-�5�-�3�2�-�5�4�4�<�/�G�r�o�u�p�I�d�>�
�
� � � � � � �<�R�u�n�L�e�v�e�l�>�H�i�g�h�e�s�t�A�v�a�i�l�a�b�l�e�<�/�R�u�n�L�e�v�e�l�>�
�
� � � � �<�/�P�r�i�n�c�i�p�a�l�>�
�
� � �<�/�P�r�i�n�c�i�p�a�l�s�>�
�
� � �<�S�e�t�t�i�n�g�s�>�
�
� � � � �<�M�u�l�t�i�p�l�e�I�n�s�t�a�n�c�e�s�P�o�l�i�c�y�>�S�t�o�p�E�x�i�s�t�i�n�g�<�/�M�u�l�t�i�p�l�e�I�n�s�t�a�n�c�e�s�P�o�l�i�c�y�>�
�
� � � � �<�D�i�s�a�l�l�o�w�S�t�a�r�t�I�f�O�n�B�a�t�t�e�r�i�e�s�>�f�a�l�s�e�<�/�D�i�s�a�l�l�o�w�S�t�a�r�t�I�f�O�n�B�a�t�t�e�r�i�e�s�>�
�
� � � � �<�S�t�o�p�I�f�G�o�i�n�g�O�n�B�a�t�t�e�r�i�e�s�>�t�r�u�e�<�/�S�t�o�p�I�f�G�o�i�n�g�O�n�B�a�t�t�e�r�i�e�s�>�
�
� � � � �<�A�l�l�o�w�H�a�r�d�T�e�r�m�i�n�a�t�e�>�t�r�u�e�<�/�A�l�l�o�w�H�a�r�d�T�e�r�m�i�n�a�t�e�>�
�
� � � � �<�S�t�a�r�t�W�h�e�n�A�v�a�i�l�a�b�l�e�>�f�a�l�s�e�<�/�S�t�a�r�t�W�h�e�n�A�v�a�i�l�a�b�l�e�>�
�
� � � � �<�R�u�n�O�n�l�y�I�f�N�e�t�w�o�r�k�A�v�a�i�l�a�b�l�e�>�f�a�l�s�e�<�/�R�u�n�O�n�l�y�I�f�N�e�t�w�o�r�k�A�v�a�i�l�a�b�l�e�>�
�
� � � � �<�I�d�l�e�S�e�t�t�i�n�g�s�>�
�
� � � � � � �<�S�t�o�p�O�n�I�d�l�e�E�n�d�>�t�r�u�e�<�/�S�t�o�p�O�n�I�d�l�e�E�n�d�>�
�
� � � � � � �<�R�e�s�t�a�r�t�O�n�I�d�l�e�>�f�a�l�s�e�<�/�R�e�s�t�a�r�t�O�n�I�d�l�e�>�
�
� � � � �<�/�I�d�l�e�S�e�t�t�i�n�g�s�>�
�
� � � � �<�A�l�l�o�w�S�t�a�r�t�O�n�D�e�m�a�n�d�>�t�r�u�e�<�/�A�l�l�o�w�S�t�a�r�t�O�n�D�e�m�a�n�d�>�
�
� � � � �<�E�n�a�b�l�e�d�>�t�r�u�e�<�/�E�n�a�b�l�e�d�>�
�
� � � � �<�H�i�d�d�e�n�>�f�a�l�s�e�<�/�H�i�d�d�e�n�>�
�
� � � � �<�R�u�n�O�n�l�y�I�f�I�d�l�e�>�f�a�l�s�e�<�/�R�u�n�O�n�l�y�I�f�I�d�l�e�>�
�
� � � � �<�D�i�s�a�l�l�o�w�S�t�a�r�t�O�n�R�e�m�o�t�e�A�p�p�S�e�s�s�i�o�n�>�f�a�l�s�e�<�/�D�i�s�a�l�l�o�w�S�t�a�r�t�O�n�R�e�m�o�t�e�A�p�p�S�e�s�s�i�o�n�>�
�
� � � � �<�U�s�e�U�n�i�f�i�e�d�S�c�h�e�d�u�l�i�n�g�E�n�g�i�n�e�>�t�r�u�e�<�/�U�s�e�U�n�i�f�i�e�d�S�c�h�e�d�u�l�i�n�g�E�n�g�i�n�e�>�
�
� � � � �<�W�a�k�e�T�o�R�u�n�>�f�a�l�s�e�<�/�W�a�k�e�T�o�R�u�n�>�
�
� � � � �<�E�x�e�c�u�t�i�o�n�T�i�m�e�L�i�m�i�t�>�P�T�1�H�<�/�E�x�e�c�u�t�i�o�n�T�i�m�e�L�i�m�i�t�>�
�
� � � � �<�P�r�i�o�r�i�t�y�>�7�<�/�P�r�i�o�r�i�t�y�>�
�
� � �<�/�S�e�t�t�i�n�g�s�>�
�
� � �<�A�c�t�i�o�n�s� �C�o�n�t�e�x�t�=�"�A�u�t�h�o�r�"�>�
�
� � � � �<�E�x�e�c�>�
�
� � � � � � �<�C�o�m�m�a�n�d�>�"�C�:�\�P�o�r�t�a�b�l�e� �A�p�p�s�\�M�o�n�O�f�f� �(�D�i�s�p�l�a�y� �o�f�f� �W�h�e�n� �L�o�c�k�e�d�)�\�M�o�n�O�f�f� �(�B�a�t�t�e�r�y� �O�n�l�y�)�.�b�a�t�"�<�/�C�o�m�m�a�n�d�>�
�
� � � � �<�/�E�x�e�c�>�
�
� � �<�/�A�c�t�i�o�n�s�>�
�
�<�/�T�a�s�k�>�

Cleaned up version (�'s removed):

ÿþ<?xml version="1.0" encoding="UTF-16"?>

<Task version="1.4" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">

  <RegistrationInfo>

    <Date>2021-01-11T04:56:22.2785148</Date>

    <Author>Computer_Name\User</Author>

    <URI>\Display Off When Locked (Battery Only)</URI>

  </RegistrationInfo>

  <Triggers>

    <SessionStateChangeTrigger>

      <Enabled>true</Enabled>

      <StateChange>SessionLock</StateChange>

    </SessionStateChangeTrigger>

  </Triggers>

  <Principals>

    <Principal id="Author">

      <GroupId>S-1-5-32-544</GroupId>

      <RunLevel>HighestAvailable</RunLevel>

    </Principal>

  </Principals>

  <Settings>

    <MultipleInstancesPolicy>StopExisting</MultipleInstancesPolicy>

    <DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>

    <StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>

    <AllowHardTerminate>true</AllowHardTerminate>

    <StartWhenAvailable>false</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>PT1H</ExecutionTimeLimit>

    <Priority>7</Priority>

  </Settings>

  <Actions Context="Author">

    <Exec>

      <Command>"C:\Portable Apps\MonOff (Display off When Locked)\MonOff (Battery Only).bat"</Command>

    </Exec>

  </Actions>

</Task>

task.xml:

ÿþ<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.4" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
  <RegistrationInfo>
    <Date>2021-01-11T04:56:22.2785148</Date>
    <Author>Computer_Name\User</Author>
    <URI>\Display Off When Locked (Battery Only)</URI>
  </RegistrationInfo>
  <Triggers>
    <SessionStateChangeTrigger>
      <Enabled>true</Enabled>
      <StateChange>SessionLock</StateChange>
    </SessionStateChangeTrigger>
  </Triggers>
  <Principals>
    <Principal id="Author">
      <GroupId>S-1-5-32-544</GroupId>
      <RunLevel>HighestAvailable</RunLevel>
    </Principal>
  </Principals>
  <Settings>
    <MultipleInstancesPolicy>StopExisting</MultipleInstancesPolicy>
    <DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
    <StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
    <AllowHardTerminate>true</AllowHardTerminate>
    <StartWhenAvailable>false</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>PT1H</ExecutionTimeLimit>
    <Priority>7</Priority>
  </Settings>
  <Actions Context="Author">
    <Exec>
      <Command>"C:\Portable Apps\MonOff (Display off When Locked)\MonOff (Battery Only).bat"</Command>
    </Exec>
  </Actions>
</Task>

As you can see, the xml exported from scheduled tasks has a lot of extra stuff (Unicode characters?) in it. The only thing I can think of at this point is to try copy/pasting the stuff from HxD into the batch file and having all those extra characters echo'd into the xml file, assuming it will even do so, which is what I'm going to try next. I need to do this in order to be able to have the scheduled task created automatically based on certain variables that may differ between computers. Any help would be much appreciated. Thanks!

2
  • 1
    @DrinkSomePimpJuiceIT - Thanks. That's actually similar to the alternate solution I came up with when trying to figure this out last night, which is included in my answer I just posted, though PowerShell isn't actually needed, as it can be done via a couple commands in batch. I'm fairly comfortable with batch and have very little experience with PowerShell, so while I realize PowerShell is often better and has become the preferred method by many, I haven't ventured into its use any more than absolutely necessary.
    – vertigo
    Commented Jan 17, 2021 at 21:58
  • 1
    @DrinkSomePimpJuiceIT - But if you wouldn't mind throwing something together that people can use in a .ps1 script as an alternative method, someone (including possibly me in the future) may find that useful, and it would certainly be appreciated.
    – vertigo
    Commented Jan 17, 2021 at 21:59

1 Answer 1

2

TL;DR - If you just want to skip to the solution and skip the context and other info included for additional help with setting things up in a similar situation, i.e. you have a specific need and just want to know how to do it, I've enclosed the info needed in #'s (+###+ indicates start of section and -###- indicates end). Thanks to user 382099/it-wasnt-me for their incredible help in figuring this out.

After quite a bit of time and trial and error, I've developed two solutions to this. The first is the actual answer to the question, credit for which goes to user 382099/it-wasnt-me who spent a lot of time patiently working with me to develop it. I helped with testing and troubleshooting, and cleaned up the code to remove parts that ended up not being necessary to accomplish the task, but they created all the code. Unfortunately, they deleted their answer, thinking I could provide a cleaner and clearer one based on our end result, so I can't select their answer as the solution, but I want to be sure to give them the credit and thank them for sharing their time and knowledge in coming up with it.

First, some context to explain what I'm doing and why. I keep all my software in D:\Programs, and while installed software goes to C:\Program Files and C:\Program Files (x86), portable software, tweaks, etc doesn't. So I'm creating "installer" batch files for use in easily setting these up on other systems, but need to account for possible differences, in this case different computer and user names (for use in the scheduled task to match the computer and user on the system it's being set up on) and possibly different portable app "install" directories (to tell the scheduled task where the files are for it to run). I then have a text file called !Portable_Apps_Directory! in the D:\Programs folder, and the contents of the file are the path of the portable apps directory (in my case I'm using C:\Portable Apps), so all that needs to be done to change the location for a different setup is to edit that one file before running all the "installers."

For this answer, I'm going to share my various scripts I created for setting up scheduled tasks to turn the display off when locking the computer, since they contain both the solution 382099/it-wasnt-me and I came up with as well as showing how I'm doing the other "install" aspects, in case someone else finds it useful for doing something similar, and it may also help others in setting their computer up to turn off the display automatically (there are fairly simply instructions online for doing this, but this may help make it a little easier by automating it). In order to do this, you must first download nircmd.exe and put it on the same drive as these batch files, as well as the previously mentioned !Portable_Apps_Directory!.txt file. There are two versions of this task, one that turns the display off when the computer is locked regardless of whether it's on AC power or battery (for maximum power conservation), and one that only does so when on battery (in case you want to be able to lock it but still be able to see the clock, e.g. for use in an office). There are therefore two "installers," each with their own secondary batch file they call for creating the applicable task xml file, and the battery only one also copies the additional batch file necessary for determining whether on AC or battery power and only turning off the display when on battery.

+###############################################################################+

First, before delving into the much more complicated real solution, the other "solution" is just another way to accomplish this, which is actually much simpler, and people looking to accomplish similar things may prefer to use. Instead of using a batch file to create and import a task, you can use an already created task and use a batch file to edit and import it. So just export it from a computer where it's already set up and put it in a directory with a batch file that does the following (keep in mind you need to run it as admin and I haven't tested it to ensure the task is created quick enough for the /change line to work):

schtasks.exe /create /xml "path\filename.xml" /tn "Task Name"
schtasks.exe /change "Task Name" /tr "'%portable_apps_dir%\file_to_run' arguments"

Another option would be to use schtasks.exe /create to build the task from scratch, for example:

schtasks /Create /RU Administrator /RL highest /TN "Windows Defender Update Test" /TR "'C:\Program Files\Windows Defender\MpCmdRun.exe' -SignatureUpdate" /SC ONEVENT /EC Microsoft-Windows-WindowsUpdateClient/Operational /MO *[System/EventID=41]

creates a task to run Windows Defender update when a Windows update is downloaded. This is necessary since MS, in their infinite wisdom, decided that if you set Windows updates to download but not install, which they all but force you to do since once installed updates force your system to restart, which is an absolute no-go, it won't even automatically update WD's definitions, with no separate option to update them (as far as I'm concerned, just one more reason to not use their crappy product, since MS apparently feels it's appropriate to try and use this as yet another method to force users into accepting automatic updates). Most people have it run daily, but I made it run whenever any update is downloaded so it will happen right away (in theory, though I'm not convinced it is). Anyways, it shows another method for creating a task via batch, one that may be obvious to many but it didn't occur to me at first, though it's more limited, since you can't set many of the options.

-################################################################################-

And here's the other solution:

Final code by user 382099/it-wasnt-me (not recommended due to having excess code that ended up not being needed, but included for credit and to preserve their work, and in case someone ends up running into issues with cleaned up code and might therefore find this useful:

@echo off

cd /d "%~dp0" && chcp 65001 >nul && set "_Portable_Apps=%~dp0"
for /f tokens^=* %%i in ('cmd /u /c ^<con: findstr /e ">" "%~dpnx0"
')do <nul (call set /p "'=%%~i"&echo\)>>"%~dpnx0TempXML.tmp"
powershell -nop -c "gc '%~dpnx0TempXML.tmp'|Out-File -FilePath '%~dp0Display Off When Locked (Battery Only).xml' -Encoding UNICODE"
echo="%_Portable_Apps\%">"%temp%\_Portable_Apps_var.txt"  & 2>nul del /q /f "%~dpnx0TempXML.tmp" & goto :eof

<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.4" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
  <RegistrationInfo>
    <Date>2021-01-11T04:56:22.2785148</Date>
    <Author>%ComputerName%\%Username%</Author>
    <URI>\Display Off When Locked (Battery Only)</URI>
  </RegistrationInfo>
  <Triggers>
    <SessionStateChangeTrigger>
      <Enabled>true</Enabled>
      <StateChange>SessionLock</StateChange>
    </SessionStateChangeTrigger>
  </Triggers>
  <Principals>
    <Principal id="Author">
      <GroupId>S-1-5-32-544</GroupId>
      <RunLevel>HighestAvailable</RunLevel>
    </Principal>
  </Principals>
  <Settings>
    <MultipleInstancesPolicy>StopExisting</MultipleInstancesPolicy>
    <DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
    <StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
    <AllowHardTerminate>true</AllowHardTerminate>
    <StartWhenAvailable>false</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>PT1H</ExecutionTimeLimit>
    <Priority>7</Priority>
  </Settings>
  <Actions Context="Author">
    <Exec>
      <Command>"%_Portable_Apps%MonOff (Battery Only).bat"</Command>
    </Exec>
  </Actions>
</Task>
rem :: this line is required... keep here.... 

+################################################################################+

Final cleaned up code, actual answer to question starting with "chcp 65001 >nul" line (Always - Put in "Make Task - Display Off When Locked (Always).bat"):

@echo off
setlocal enableDelayedExpansion

REM set batch file path to variable since %~dp0 is bugged and changes to %cd% when changing directories if %~dp0 is quoted and therefore can't be relied on
set "dp0=%~dp0"
cd /d "%dp0%"
for %%I in (.) do set "dest_dir=%%~nxI"

:loop_portable_apps_dir
dir "^!Portable_Apps_Directory^!.txt"
if %errorlevel% equ 1 (
   REM Check if in root directory to keep from trying to move up forever
   if "%cd:~3,1%"=="" (
      cls
      echo ERROR: !Portable_Apps_Directory!.txt NOT FOUND, MAKE SURE FILE WITH THIS NAME EXISTS IN A PARENT DIRECTORY OF THIS DIRECTORY ( "%dp0%" ^) AND THAT IT INCLUDES THE PATH TO THE DESIRED PORTABLE APPS DIRECTORY.
      pause
      endlocal & exit /b
   ) else (
      cd..
      goto loop_portable_apps_dir
   )
) else (
   SET /p portable_apps_dir=<"^!Portable_Apps_Directory^!.txt"
)

for /f "tokens=*" %%a in ('where /r "%portable_apps_dir%" nircmd.exe') do set nircmd=%%a

if EXIST "%dp0%\Display Off When Locked (Always) Scheduled Task.xml" del "%dp0%\Display Off When Locked (Always) Scheduled Task.xml"

chcp 65001 >nul
for /f tokens^=* %%i in ('cmd /u /c ^<con: findstr /e ">" "%dp0%\%~nx0"') do <nul (call set /p "'=%%~i"&echo\)>>"%dp0%\Display Off When Locked (Always) Scheduled Task.xml"
goto :eof

<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.4" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
  <RegistrationInfo>
    <Date>2021-01-11T04:56:22.2785148</Date>
    <Author>%ComputerName%\%Username%</Author>
    <URI>\Display Off When Locked (Always)</URI>
  </RegistrationInfo>
  <Triggers>
    <SessionStateChangeTrigger>
      <Enabled>true</Enabled>
      <StateChange>SessionLock</StateChange>
    </SessionStateChangeTrigger>
  </Triggers>
  <Principals>
    <Principal id="Author">
      <GroupId>S-1-5-32-544</GroupId>
      <RunLevel>HighestAvailable</RunLevel>
    </Principal>
  </Principals>
  <Settings>
    <MultipleInstancesPolicy>StopExisting</MultipleInstancesPolicy>
    <DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
    <StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
    <AllowHardTerminate>true</AllowHardTerminate>
    <StartWhenAvailable>false</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>PT1H</ExecutionTimeLimit>
    <Priority>7</Priority>
  </Settings>
  <Actions Context="Author">
    <Exec>
      <Command>"%nircmd%"</Command>
      <Arguments>monitor off</Arguments>
    </Exec>
  </Actions>
</Task>
REM DON'T DELETE THIS LINE

Final cleaned up code, actual answer to question starting with "chcp 65001 >nul" line (Battery Only - Put in "Make Task - Display Off When Locked (Battery Only).bat"):

@echo off
setlocal enableDelayedExpansion

REM set batch file path to variable since %~dp0 is bugged and changes to %cd% when changing directories if %~dp0 is quoted and therefore can't be relied on
set "dp0=%~dp0"
cd /d "%dp0%"
for %%I in (.) do set "dest_dir=%%~nxI"

:loop_portable_apps_dir
dir "^!Portable_Apps_Directory^!.txt"
if %errorlevel% equ 1 (
   REM Check if in root directory to keep from trying to move up forever
   if "%cd:~3,1%"=="" (
      cls
      echo ERROR: !Portable_Apps_Directory!.txt NOT FOUND, MAKE SURE FILE WITH THIS NAME EXISTS IN A PARENT DIRECTORY OF THIS DIRECTORY ( "%dp0%" ^) AND THAT IT INCLUDES THE PATH TO THE DESIRED PORTABLE APPS DIRECTORY.
      pause
      endlocal & exit /b
   ) else (
      cd..
      goto loop_portable_apps_dir
   )
) else (
   SET /p portable_apps_dir=<"^!Portable_Apps_Directory^!.txt"
)

if EXIST "%dp0%\Display Off When Locked (Battery Only) Scheduled Task.xml" del "%dp0%\Display Off When Locked (Battery Only) Scheduled Task.xml"

chcp 65001 >nul
for /f tokens^=* %%i in ('cmd /u /c ^<con: findstr /e ">" "%dp0%\%~nx0"') do <nul (call set /p "'=%%~i"&echo\)>>"%dp0%\Display Off When Locked (Battery Only) Scheduled Task.xml"
goto :eof

<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.4" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
  <RegistrationInfo>
    <Date>2021-01-11T04:56:22.2785148</Date>
    <Author>%ComputerName%\%Username%</Author>
    <URI>\Display Off When Locked (Battery Only)</URI>
  </RegistrationInfo>
  <Triggers>
    <SessionStateChangeTrigger>
      <Enabled>true</Enabled>
      <StateChange>SessionLock</StateChange>
    </SessionStateChangeTrigger>
  </Triggers>
  <Principals>
    <Principal id="Author">
      <GroupId>S-1-5-32-544</GroupId>
      <RunLevel>HighestAvailable</RunLevel>
    </Principal>
  </Principals>
  <Settings>
    <MultipleInstancesPolicy>StopExisting</MultipleInstancesPolicy>
    <DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
    <StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
    <AllowHardTerminate>true</AllowHardTerminate>
    <StartWhenAvailable>false</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>PT1H</ExecutionTimeLimit>
    <Priority>7</Priority>
  </Settings>
  <Actions Context="Author">
    <Exec>
      <Command>"%portable_apps_dir%\%dest_dir%\MonOff (Battery Only).bat"</Command>
    </Exec>
  </Actions>
</Task>
REM DON'T DELETE THIS LINE

-################################################################################-

And here's the code for the additional files necessary for the whole setup as described above:

"!Setup (display off always - RUN AS ADMIN).bat":

@echo off
setlocal enableDelayedExpansion

REM set batch file path to variable since %~dp0 is bugged and changes to %cd% when changing directories if %~dp0 is quoted and therefore can't be relied on
set "dp0=%~dp0"
cd /d "%dp0%"

net session 1>nul 2>nul
if %errorlevel% neq 0 cls & echo. & echo ERROR: RUN SCRIPT AS ADMINISTRATOR & echo. & pause & endlocal & exit /b

:loop_nircmd_dir
where /r . nircmd.exe 1>nul 2>nul
if %errorlevel% equ 1 (
   REM Check if in root directory to keep from trying to move up forever
   if "%cd:~3,1%"=="" (
      cls
      echo ERROR: nircmd.exe NOT FOUND, MAKE SURE FILE WITH THIS NAME EXISTS ON THIS DRIVE.
      pause
      endlocal & exit /b
   ) else (
      cd..
      goto loop_nircmd_dir
   )
) else (
   for /f "tokens=*" %%a in ('where /r .. nircmd.exe') do set nircmd=%%a
   for %%A in ("!nircmd!") do set nircmd_path=%%~dpA
   cd "!nircmd_path!"
   for %%I in (.) do set "nircmd_dir_name=%%~nxI"
   set "nircmd_path=!cd!"
   cd /d "%dp0%"
)

:loop_portable_apps_dir
dir "^!Portable_Apps_Directory^!.txt"
if %errorlevel% equ 1 (
   REM Check if in root directory to keep from trying to move up forever
   if "%cd:~3,1%"=="" (
      cls
      echo ERROR: !Portable_Apps_Directory!.txt NOT FOUND, MAKE SURE FILE WITH THIS NAME EXISTS IN A PARENT DIRECTORY OF THIS DIRECTORY ( "%dp0%" ^) AND THAT IT INCLUDES THE PATH TO THE DESIRED PORTABLE APPS DIRECTORY.
      pause
      endlocal & exit /b
   ) else (
      cd..
      goto loop_portable_apps_dir
   )
) else (
   SET /p portable_apps_dir=<"^!Portable_Apps_Directory^!.txt"
)

mkdir "%portable_apps_dir%\%nircmd_dir_name%"
robocopy /e "%nircmd_path%" "%portable_apps_dir%\%nircmd_dir_name%"
if %errorlevel% gtr 7 echo. & echo ERROR COPYING WITH ROBOCOPY & echo. & pause & endlocal & exit /b

call "%dp0%\Make Task - Display Off When Locked (Always).bat"
schtasks.exe /Delete /f /tn "Display Off When Locked (Battery Only)" 1>nul 2>nul
schtasks.exe /Delete /f /tn "Display Off When Locked (Always)" 1>nul 2>nul
schtasks.exe /Create /XML "%dp0%\Display Off When Locked (Always) Scheduled Task.xml" /tn "Display Off When Locked (Always)"
if %errorlevel% equ 1 (
   if NOT EXIST "%dp0%\Display Off When Locked (Always) Scheduled Task.xml" (
      cls & echo UNABLE TO CREATE TASK - "%dp0%\Display Off When Locked (Always) Scheduled Task.xml" SHOULD BE CREATED BY THIS SCRIPT BUT DOES NOT EXIST. & echo. & pause
   ) else (
      cls & echo UNABLE TO CREATE TASK - ACCESS DENIED OR PROBLEM WITH "%dp0%\Display Off When Locked (Always) Scheduled Task.xml" RESULTING IN UNSUCCESSFUL IMPORT. RUN SCRIPT AS ADMINISTRATOR, IF STILL GETTING THIS ERROR, CHECK XML FILE AGAINST "%dp0%\-Display Off When Locked (Always) Scheduled Task- TEMPLATE.xml" FOR ERRORS. & echo. & set /p "user_input=Open files in notepad for comparison? [Y/N]"
      if NOT "%user_input%" == "Y" if NOT "%user_input%" == "y" (
         REM Do nothing
      ) else (
         start notepad.exe "%dp0%\Display Off When Locked (Always) Scheduled Task.xml"
         if EXIST "%dp0%\-Display Off When Locked (Always) Scheduled Task - TEMPLATE.xml" start notepad.exe "%dp0%\-Display Off When Locked (Always) Scheduled Task - TEMPLATE.xml"
      )
   )
   cls & echo. & echo SUCCESS^^! & echo. & timeout /t 5
)

endlocal & exit /b

"!Setup (display off only when on battery - RUN AS ADMIN).bat":

@echo off
setlocal enableDelayedExpansion

REM set batch file path to variable since %~dp0 is bugged and changes to %cd% when changing directories if %~dp0 is quoted and therefore can't be relied on
set "dp0=%~dp0"
cd /d "%dp0%"
for %%I in (.) do set "dest_dir=%%~nxI"

net session 1>nul 2>nul
if %errorlevel% neq 0 cls & echo. & echo ERROR: RUN SCRIPT AS ADMINISTRATOR & echo. & pause & endlocal & exit /b

:loop_nircmd_dir
where /r . nircmd.exe 1>nul 2>nul
if %errorlevel% equ 1 (
   REM Check if in root directory to keep from trying to move up forever
   if "%cd:~3,1%"=="" (
      cls
      echo ERROR: nircmd.exe NOT FOUND, MAKE SURE FILE WITH THIS NAME EXISTS ON THIS DRIVE.
      pause
      endlocal & exit /b
   ) else (
      cd..
      goto loop_nircmd_dir
   )
) else (
   for /f "tokens=*" %%a in ('where /r .. nircmd.exe') do set nircmd=%%a
   for %%A in ("!nircmd!") do set nircmd_path=%%~dpA
   cd "!nircmd_path!"
   for %%I in (.) do set "nircmd_dir_name=%%~nxI"
   set "nircmd_path=!cd!"
   cd /d "%dp0%"
)

:loop_portable_apps_dir
dir "^!Portable_Apps_Directory^!.txt"
if %errorlevel% equ 1 (
   REM Check if in root directory to keep from trying to move up forever
   if "%cd:~3,1%"=="" (
      cls
      echo ERROR: !Portable_Apps_Directory!.txt NOT FOUND, MAKE SURE FILE WITH THIS NAME EXISTS IN A PARENT DIRECTORY OF THIS DIRECTORY ( "%dp0%" ^) AND THAT IT INCLUDES THE PATH TO THE DESIRED PORTABLE APPS DIRECTORY.
      pause
      endlocal & exit /b
   ) else (
      cd..
      goto loop_portable_apps_dir
   )
) else (
   SET /p portable_apps_dir=<"^!Portable_Apps_Directory^!.txt"
)

mkdir "%portable_apps_dir%\%dest_dir%"
copy /y "%dp0%\MonOff (Battery Only).bat" "%portable_apps_dir%\%dest_dir%"
mkdir "%portable_apps_dir%\%nircmd_dir_name%"
robocopy /e "%nircmd_path%" "%portable_apps_dir%\%nircmd_dir_name%"
if %errorlevel% gtr 7 echo. & echo ERROR COPYING WITH ROBOCOPY & echo. & pause & endlocal & exit /b

call "%dp0%\Make Task - Display Off When Locked (Battery Only).bat"
schtasks.exe /Delete /f /tn "Display Off When Locked (Always)" 1>nul 2>nul
schtasks.exe /Delete /f /tn "Display Off When Locked (Battery Only)" 1>nul 2>nul
schtasks.exe /Create /XML "%dp0%\Display Off When Locked (Battery Only) Scheduled Task.xml" /tn "Display Off When Locked (Battery Only)"
if %errorlevel% equ 1 (
   if NOT EXIST "%dp0%\Display Off When Locked (Battery Only) Scheduled Task.xml" (
      cls & echo UNABLE TO CREATE TASK - "%dp0%\Display Off When Locked (Battery Only) Scheduled Task.xml" SHOULD BE CREATED BY THIS SCRIPT BUT DOES NOT EXIST. & echo. & pause
   ) else (
      cls & echo UNABLE TO CREATE TASK - ACCESS DENIED OR PROBLEM WITH "%dp0%\Display Off When Locked (Battery Only) Scheduled Task.xml" RESULTING IN UNSUCCESSFUL IMPORT. RUN SCRIPT AS ADMINISTRATOR, IF STILL GETTING THIS ERROR, CHECK XML FILE AGAINST "%dp0%\-Display Off When Locked (Battery Only) Scheduled Task- TEMPLATE.xml" FOR ERRORS. & echo. & set /p "user_input=Open files in notepad for comparison? [Y/N]"
      if NOT "%user_input%" == "Y" if NOT "%user_input%" == "y" (
         REM Do nothing
      ) else (
         start notepad.exe "%dp0%\Display Off When Locked (Battery Only) Scheduled Task.xml"
         if EXIST "%dp0%\-Display Off When Locked (Battery Only) Scheduled Task - TEMPLATE.xml" start notepad.exe "%dp0%\-Display Off When Locked (Battery Only) Scheduled Task - TEMPLATE.xml"
      )
   )
   cls & echo. & echo SUCCESS^^! & echo. & timeout /t 5
)

endlocal & exit /b

"MonOff (Battery Only).bat":

@echo off
setlocal enableDelayedExpansion

REM Only turn off monitor when on battery - Set scheduled task to run "nircmd.exe" (with quotes) with "monitor off" (without quotes) in argument to always turn monitor off
set "cmd=WMIC /NameSpace:\\root\WMI Path BatteryStatus Get PowerOnline"
%cmd% | find /i "true" > nul
if %errorlevel% EQU 1 (
   for /f "tokens=*" %%a in ('where /r .. nircmd.exe') do set nircmd=%%a
   start /b "" "!nircmd!" monitor off
)

endlocal & exit /b

"-Display Off When Locked (Always) Scheduled Task - TEMPLATE.xml":

<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.4" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<RegistrationInfo>
<Date>2021-01-11T04:56:22.2785148</Date>
<Author>COMPUTER_NAME\Username</Author>
<URI>\Display Off When Locked (Always)</URI>
</RegistrationInfo>
<Triggers>
<SessionStateChangeTrigger>
<Enabled>true</Enabled>
<StateChange>SessionLock</StateChange>
</SessionStateChangeTrigger>
</Triggers>
<Principals>
<Principal id="Author">
<GroupId>S-1-5-32-544</GroupId>
<RunLevel>HighestAvailable</RunLevel>
</Principal>
</Principals>
<Settings>
<MultipleInstancesPolicy>StopExisting</MultipleInstancesPolicy>
<DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
<AllowHardTerminate>true</AllowHardTerminate>
<StartWhenAvailable>false</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>PT1H</ExecutionTimeLimit>
<Priority>7</Priority>
</Settings>
<Actions Context="Author">
<Exec>
<Command>"C:\Portable Apps\NirCmd x64 (Portable)\nircmd.exe"</Command>
<Arguments>monitor off</Arguments>
</Exec>
</Actions>
</Task>

"-Display Off When Locked (Battery Only) Scheduled Task - TEMPLATE":

<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.4" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<RegistrationInfo>
<Date>2021-01-11T04:56:22.2785148</Date>
<Author>COMPUTER_NAME\Username</Author>
<URI>\Display Off When Locked (Battery Only)</URI>
</RegistrationInfo>
<Triggers>
<SessionStateChangeTrigger>
<Enabled>true</Enabled>
<StateChange>SessionLock</StateChange>
</SessionStateChangeTrigger>
</Triggers>
<Principals>
<Principal id="Author">
<GroupId>S-1-5-32-544</GroupId>
<RunLevel>HighestAvailable</RunLevel>
</Principal>
</Principals>
<Settings>
<MultipleInstancesPolicy>StopExisting</MultipleInstancesPolicy>
<DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
<AllowHardTerminate>true</AllowHardTerminate>
<StartWhenAvailable>false</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>PT1H</ExecutionTimeLimit>
<Priority>7</Priority>
</Settings>
<Actions Context="Author">
<Exec>
<Command>"C:\Portable Apps\MonOff (Display off When Locked)\MonOff (Battery Only).bat"</Command>
</Exec>
</Actions>
</Task>

One thing that may be an issue is that when using a task created on one system as a template for recreating it on another system, the may not be relevant to the new system, and so it might not work. If anybody tries this, it would be interesting to know whether it works on their computer or if that proves to be an issue. If it is, I think the solution is

schtasks /Change /RU {username/Administrator}

though that asks for the password, so the script wouldn't be fully automated, and if the admin password is blank, which I'm guessing it is on most user/consumer systems (user would (hopefully) have a password, but that's not the same as the admin password, which isn't typically going to be set separately), it says the task may not run depending on group policy.

2
  • As I mentioned in the chat, your English will explain better than mine, and more than 70% of the solution is not due to the code, but rather your perception in the process, very good! +1
    – Io-oI
    Commented Jan 17, 2021 at 22:21
  • I'm here to learn what I can, where is possible, your explanation is clearer than mine, and much of the solution is due to your attentive perception, so I understand that you have more merits than I do. My username tends to anonymity, where any productivity does not seek to claim author.
    – Io-oI
    Commented Jan 17, 2021 at 22:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .