Notes on Chocolatey

With the Chocolatey Package Manager for Windows, it is super easy to install software from the command-line. This makes your installs scriptable and thus repeatable. In this blog I'll show you how to render installation instructions from a machine and how to use the Windows Task Scheduler to update your packages regularly.

Listing installation instructions

Let's create a list with installation instructions from a machine:

choco list -l -r --id-only `
| Where-Object { $_ -notmatch '^(KB|chocolatey|vcredist)' } `
| ForEach-Object { echo "choco install -y $PSItem" }

Note: I filter out packages that are knowledge base items, belonging to Chocolatey itself or obvious dependencies.

Upgrade Task

I love software that is updated to the latest version. The following PowerShell script will generate a task that will trigger an upgrade every Monday at 9:30 in the morning:

$day = "Monday"
$time = "9:30am"
$taskName = "UpgradeChocolateyPackages"

# Check if the task exists:
$taskExists = Get-ScheduledTask | Where-Object { $_.TaskName -eq $taskName }

if ($taskExists) {
    # Unregister the existing task with the same name:
    Unregister-ScheduledTask -TaskName $taskName -Confirm:$false

# Register task the launches in an elevated powershell, user must confirm action
$action = New-ScheduledTaskAction `
    -Execute "powershell.exe" `
    -Argument "-Command ""Start-Process powershell -ArgumentList '-Command & {choco upgrade all -y}' -Verb RunAs"""

$trigger = New-ScheduledTaskTrigger `
    -Weekly `
    -DaysOfWeek $day `
    -At $time

$settings = New-ScheduledTaskSettingsSet `
    -AllowStartIfOnBatteries `
    -DontStopIfGoingOnBatteries `
    -StartWhenAvailable `

# register task using all the setting
Register-ScheduledTask `
    -Action $action `
    -Trigger $trigger `
    -TaskName $taskName `
    -Description "Upgrade all Chocolatey packages" `
    -Settings $settings

Make Upgrade Task Compatible with MakeMeAdmin

At Wehkamp we use Make Me Admin. You need to become admin explicitly before executing administrative tasks. The upgrade script will not work in this case, because it directly shows you the prompt to login -- not allowing you to become admin first. So we need an extra step confirm step in our action:

$action = New-ScheduledTaskAction `
    -Execute "powershell.exe" `
    -Argument "-Command ""Read-Host ""Make sure you are admin before proceeding...""; Start-Process powershell -ArgumentList '-Command & {choco upgrade all -y}' -Verb RunAs"""

Now the user is prompted before the upgrade is executed.

My arbitrary list of installed packages

I use my blogs as notes to my future self. When I get a new machine, I usually install the following packages:

# dev tools
## for AWS
## more info here:
choco install -y awscli aws-vault
## docker
choco install -y ranger-desktop
## IDE's
choco install -y vscode.install
# compare
choco install -y beyondcompare
choco install -y filezilla
## git and git signing
## more info here:
choco install -y git.install gpg4win
## testing APIs
choco install -y postman
## edit YAML files
choco install -y yq
## Node.js
choco install -y nvm.install

# image editing tools
## diagrams
choco install -y drawio
## for svgs: 
choco install -y InkScape
## better paint:
choco install -y
## screen recordings
choco install -y ffmpeg screentogif.portable

# tools
## authy for 2FA
choco install -y authy-desktop
## where did my diskspace go?
choco install -y treesizefree
## make Windows behave better
choco install -y powertoys

## others
choco install -y powertoys
choco install -y slack