How to create a Scheduled Task using PowerShell

Working with group Managed Service Accounts gMSA (not a topic of this post) and Scheduled Tasks, you’ll figure pretty soon that creating a Scheduled Task using the MMC Snap-in does not get you too far. As a solution: just create your Scheduled Task using Powershell.

I’m therefore using the code below. Just adjust it to your needs as required.

1
2
3
4
5
6
7
8
$taskName = "My Scheduled Task"
$taskDesc = "My fanc description"
$actionArgs = '-file "C:\Path\to\my\Script.ps1"'
$taskAction = New-ScheduledTaskAction -Execute "powershell.exe" -Argument $actionArgs 
$taskTrigger = New-ScheduledTaskTrigger -Daily -At 8:30pm
$taskSettings = New-ScheduledTaskSettingsSet -ExecutionTimeLimit (New-TimeSpan -Minutes 60) -RestartCount 2 -RestartInterval (New-TimeSpan -Minutes 15) -RunOnlyIfNetworkAvailable -Compatibility "Win8"
$taskPrincipal = New-ScheduledTaskPrincipal -UserID "CUSTOMERS\svcgMSA_Task$" -LogonType Password -RunLevel Highest
Register-ScheduledTask -TaskName $taskName -Description $taskDesc -Action $taskAction -Trigger $taskTrigger -Settings $taskSettings -Principal $taskPrincipal

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.