3

I have this script to create a scheduled task in using PowerShell which works fine however I can't figure out how to "Set and expiry date", "Run with highest privileges" and "stop the task if its running longer then".

$taskName = "TestTask"
$user = "<....>"
$password = "<...>"
$action = New-ScheduledTaskAction -Execute "notepad.exe" 
$trigger = New-ScheduledTaskTrigger -Weekly -AT "23:00" -DaysOfWeek 'Monday', 'Tuesday', 'Wednesday'
$settings = New-ScheduledTaskSettingsSet 
$inputObject = New-ScheduledTask -Action $action -Trigger $trigger -Settings $settings 
Register-ScheduledTask -TaskName $taskName -InputObject $inputObject -User $user -Password $password
0

1 Answer 1

7

You need to specify the appropriate parameters on the New-ScheduledTaskSettingsSet command e.g.

$settings = New-ScheduledTaskSettingsSet `
                -ExecutionTimeLimit ([TimeSpan]::FromHours(2)) `
                -DeleteExpiredTaskAfter ([TimeSpan]::FromDays(60) 
Register-ScheduledTask ... -Settings $settings -RunLevel Highest

Look at this help topic for more info on the New-ScheduledTaskSettingsSet command.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.