Run a powershell script at start up
The following sequence of Powershell commands configures the task schuduler such that it starts a powershell script when the system starts up:
$act = new-scheduledTaskAction -execute powershell.exe -argument '-file c:\project-X\Health-monitor.ps1'
$trg = new-scheduledTaskTrigger -atStartup
$prc = new-scheduledTaskPrincipal -userId "NT AUTHORITY\SYSTEM" -logonType ServiceAccount
$set = new-scheduledTaskSettingsSet -executionTimeLimit (new-timeSpan -seconds 0) # Make sure task is not stopped if it runs longer than a given period.
register-scheduledTask -taskName 'Health monitor' -action $act -trigger $trg -settings $set -principal $prc # -runLevel Highest