Rebuilding your PC is always a drag, even with useful utilities like Ninite.
I recently created a PowerShell DSC script that I can use whenever I need to rebuild my PC. As part of that, I used the cChoco provider to automatically install applications using Chocolatey. I’ll be writing a blog post with more details shortly.
That’s a great way to get the applications installed, but not for keeping them up-to-date. Chocolatey allows you to run ‘choco upgrade all’ manually to do this:
Rather than manually create the scheduled task to automate this, I created this short PowerShell script:
# 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 |
The script will:
- Locate the choco.exe binary (It’ll quit if it can’t find it in the path)
- Set up a scheduled task that runs said binary at system startup
Note that this script will only work on Windows 8 and newer machines, because it relies on the *-ScheduledTask cmdlets.
We will probably need to pull in cChoco into Chocolatey namespace on Github like we have the other providers. My understanding is that it is good, but it needs some more things to make it more complete.
LikeLike
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
LikeLike
Nice script. Since I work with mobile devices a lot, I’ve added two options to $taskSettings in my own variant of your script:
-RunOnlyIfNetworkAvailable: no upgrade info will be available if there is no network connection
– AllowStartIfOnBatteries: This defaults to ‘no’ for scheduled tasks, so if you want tasks to run while working on battery power, explicitly add this option.
LikeLike
Thanks Marcus. I haven’t used this script since I originally created it because I moved to a new job that had a Windows 7 fleet. Looking forward to dusting it off again in a few months when I move us onto Windows 10.
LikeLiked by 1 person