Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Cosmologist/ed0ad02e6c9e68e9b4a6e3a62ccbd7d6 to your computer and use it in GitHub Desktop.
Save Cosmologist/ed0ad02e6c9e68e9b4a6e3a62ccbd7d6 to your computer and use it in GitHub Desktop.
PowerShell: Schedule and unschedule task example (Windows cron)
$TASKNAME = "my-python-task"
$PYTHON = Resolve-Path -Path ".\venv\Scripts\python.exe"
$MAIN = Resolve-Path -Path ".\main.py"
$WORKINGDIR = Resolve-Path -Path ".\"
# Schedule
$Action = New-ScheduledTaskAction -Execute $PYTHON -Argument $MAIN -WorkingDirectory $WORKINGDIR
$Trigger = New-ScheduledTaskTrigger -Daily -At 3am
$Settings = New-ScheduledTaskSettingsSet
$Task = New-ScheduledTask -Action $Action -Trigger $Trigger -Settings $Settings
Register-ScheduledTask -TaskName $TASKNAME -InputObject $Task
# Unschedule
Unregister-ScheduledTask -TaskName $TASKNAME -Confirm:$false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment