Power Shell script to create a new Windows Task Scheduler entry

Zhu, Ligong 260 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 for business Windows Server User experience PowerShell
Windows for business Windows Client for IT Pros User experience Other
{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.