Skip to content

Instantly share code, notes, and snippets.

@seyerj
Created March 13, 2024 16:27
Show Gist options
  • Save seyerj/947d82adf59776678783c8ec72f101c8 to your computer and use it in GitHub Desktop.
Save seyerj/947d82adf59776678783c8ec72f101c8 to your computer and use it in GitHub Desktop.
Scheduled task create to update all chocolatey and winget apps at login.
# Define the commands to be scheduled
$commands = @(
"winget upgrade --all --accept-source-agreements --accept-package-agreements",
"choco upgrade all -y"
)
# Define common task properties
$taskAction = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument '{0}'
foreach ($command in $commands) {
# Extract the command name for task naming
$taskName = "UpgradeTask_" + ($command -split " ")[0]
# Create scheduled task for each command, runs as logged in user (Initial script needs to be run as admin)
$taskActionWithCommand = $taskAction -f $command
Register-ScheduledJob -Name $taskName -ScriptBlock {$taskActionWithCommand} -Trigger (New-JobTrigger -AtLogOn)
Write-Host "Scheduled task '$taskName' created successfully."
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment