Power Shell script to create a new Windows Task Scheduler entry

Zhu, Ligong 220 Reputation points
2024-09-24T18:28:26.9366667+00:00

Hi,

I wrote a powershell script to create a new task in Windows Task Scheduler:

========================================

$action = New-ScheduledTaskAction -Execute "javaw" -Argument "-jar C:\caa-sourcetree\eqa\eQAclientApp\target\caalocalservice-exec.jar"

$trigger = New-ScheduledTaskTrigger -AtLogon

$principal = New-ScheduledTaskPrincipal -UserId "$($env:USERDOMAIN)$($env:USERNAME)" -LogonType ServiceAccount

$settings = New-ScheduledTaskSettingsSet

$task = New-ScheduledTask -Action $action -Principal $principal -Trigger $trigger -Settings $settings

Register-ScheduledTask CAATask -InputObject $task

========================================

I ran it inside Windows PowerShell with admin right and it did create a new task and the program works as I expected.

The only issue I have with new task is this condition:

User's image

This task needs to run regardless AC or DC power. Can you tell me how to modify my script so that it can create the task regardless AC or battery? We use Windows 11 Enterprise machines.

Your help will be appreciated.

Windows
Windows
A family of Microsoft operating systems that run across personal computers, tablets, laptops, phones, internet of things devices, self-contained mixed reality headsets, large collaboration screens, and other devices.
5,402 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,525 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.