I'm setting up scheduled tasks in Windows Server 2016 through Powershell (not through schtasks.exe
).
$user = "Administrator"
$trigger = New-ScheduledTaskTrigger -Daily -At 0:00
$action = New-ScheduledTaskAction -Execute "C:\Windows\System32\forfiles.exe" -Argument '/p "C:\example"'
Register-ScheduledTask -Action $action -Trigger $trigger -User $user -TaskName "foobar"
The tasks do not run. Run only if user is logged in
is enabled on the new tasks. I want to try to see if the tasks run if Run whether user is logged in or not
is enabled.
I don't see anything in the New-ScheduledTaskSettingsSet documentation regarding this.
Is there a way to do this through Powershell?
$user
line, add this line:$principal = New-ScheduledTaskPrincipal -UserID "NT AUTHORITY\SYSTEM" -LogonType ServiceAccount -RunLevel Highest
and then in the register statement, remove-user $user
and replace with-Principal $principal
NT AUTHORITY\LocalSYSTEM
first before tryingSYSTEM
as localsystem has lesser privileges.NT AUTHORITY\LocalService
could work as well. You will need to run PowerShell as administrator.Register-ScheduledTask
command as in the question, and just add-Password "password"
for the associated user.