How to Schedule a Task as Local System

Use Task Scheduler instead of the obsolete at command. A Local System task runs with extensive local privileges and no interactive desktop, so its executable, arguments and working directory must all be explicit.

Prepare an administrator-controlled script

For a test, place a reviewed heartbeat.ps1 in C:\ProgramData\AdminTasks. Restrict that directory and the script so ordinary users cannot replace what SYSTEM will execute. The script can contain:

PowerShell
$logPath = 'C:\ProgramData\AdminTasks\heartbeat.log'
Add-Content -LiteralPath $logPath -Value ([DateTime]::UtcNow.ToString('o')) -Encoding UTF8 -ErrorAction Stop

Register the task

Run the following in elevated Windows PowerShell. It refuses to replace an existing task with the same name.

PowerShell
$taskName = 'Demo-AdminHeartbeat'
if (Get-ScheduledTask -TaskName $taskName -ErrorAction SilentlyContinue) {
    throw 'The demo task already exists; inspect it before continuing.'
}
$engine = Join-Path $env:windir 'System32\WindowsPowerShell\v1.0\powershell.exe'
$action = New-ScheduledTaskAction -Execute $engine -Argument '-NoProfile -NonInteractive -File "C:\ProgramData\AdminTasks\heartbeat.ps1"' -WorkingDirectory 'C:\ProgramData\AdminTasks'
$trigger = New-ScheduledTaskTrigger -Weekly -WeeksInterval 1 -DaysOfWeek Monday,Wednesday,Friday -At '10:05AM'
$principal = New-ScheduledTaskPrincipal -UserId 'SYSTEM' -LogonType ServiceAccount
Register-ScheduledTask -TaskName $taskName -Action $action -Trigger $trigger -Principal $principal

Verify identity and execution

PowerShell
(Get-ScheduledTask -TaskName $taskName).Principal
Start-ScheduledTask -TaskName $taskName
Get-ScheduledTaskInfo -TaskName $taskName

Wait for the task to finish and read its information again. Confirm the SYSTEM principal, the expected run time, a successful last result and a new UTC line in the log. Registration alone proves none of these execution outcomes.

The schedule uses the machine's local time. SYSTEM has a different profile and environment from your account. Mapped drives and interactive prompts are unsuitable. Respect the machine's script-execution policy rather than adding an execution-policy bypass to the task.

When finished with the demonstration, remove only Demo-AdminHeartbeat with Unregister-ScheduledTask -TaskName $taskName, then review its demo files before deleting them. Use a less privileged service identity when the real operation does not require SYSTEM.

References

New-ScheduledTaskPrincipal (ScheduledTasks)
Use this topic to help manage Windows and Windows Server technologies with Windows PowerShell.
https://learn.microsoft.com/en-us/powershell/module/scheduledtasks/new-scheduledtaskprincipal
New-ScheduledTaskPrincipal
Register-ScheduledTask (ScheduledTasks)
Use this topic to help manage Windows and Windows Server technologies with Windows PowerShell.
https://learn.microsoft.com/en-us/powershell/module/scheduledtasks/register-scheduledtask
Register-ScheduledTask

Author: Herman Schoenfeld