Created
December 3, 2024 21:02
-
-
Save Cosmologist/ed0ad02e6c9e68e9b4a6e3a62ccbd7d6 to your computer and use it in GitHub Desktop.
PowerShell: Schedule and unschedule task example (Windows cron)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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