Skip to content

Instantly share code, notes, and snippets.

@dstreefkerk
Last active March 8, 2022 20:52
Show Gist options
  • Save dstreefkerk/f3f0a3185aad754382bc to your computer and use it in GitHub Desktop.
Save dstreefkerk/f3f0a3185aad754382bc to your computer and use it in GitHub Desktop.
PowerShell script to create a scheduled task that runs a choco upgrade all at machine startup
# See if choco.exe is available. If not, stop execution
$chocoCmd = Get-Command -Name 'choco' -ErrorAction SilentlyContinue -WarningAction SilentlyContinue | Select-Object -ExpandProperty Source
if ($chocoCmd -eq $null) { break }
# Settings for the scheduled task
$taskAction = New-ScheduledTaskAction –Execute $chocoCmd -Argument 'upgrade all -y'
$taskTrigger = New-ScheduledTaskTrigger -AtStartup
$taskUserPrincipal = New-ScheduledTaskPrincipal -UserId 'SYSTEM'
$taskSettings = New-ScheduledTaskSettingsSet -Compatibility Win8
# Set up the task, and register it
$task = New-ScheduledTask -Action $taskAction -Principal $taskUserPrincipal -Trigger $taskTrigger -Settings $taskSettings
Register-ScheduledTask -TaskName 'Run a Choco Upgrade All at Startup' -InputObject $task -Force
@oysstu
Copy link

oysstu commented Jul 7, 2016

Tried to change the trigger to daily at 3am.
$taskTrigger = New-ScheduledTaskTrigger -Daily -At 3am
This caused the following error: "New-ScheduledTaskTrigger : Method "NewTriggerByDaily" not found"

The error can be resolved by running the following command in PS:
mofcomp C:\Windows\System32\wbem\SchedProv.mof

Ref: https://powershell.org/forums/topic/method-newtriggerbyonce-not-found-taskscheduler/

@bcurran3
Copy link

bcurran3 commented Jan 7, 2017

I think this is very useful when setting up and installing a computer with Chocolatey. I made a simple package so it will be more accessible to all. Thanks for your work and feel free to request to be a maintainer!
https://chocolatey.org/packages/chocoupgradeallatstartup/2016.02.23

@noaho
Copy link

noaho commented Jul 7, 2017

I wonder if it's possible to use the same automatic windows defined by Windows 8+ automatic update eg https://msdn.microsoft.com/en-us/library/windows/desktop/jj835985(v=vs.85).aspx ?

"You can convert any Task Scheduler task to a maintenance task. To do so, you must confirm that your application can be suspended. Then, you must extend the task definition with the new MaintenanceSettings and AllowStartOnDemand elements."

Would chocolatey exit cleanly if halted during an update? Or leave things half-upgraded..?

@zipzopzubitybop
Copy link

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment