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 `
    -RunOnlyIfNetworkAvailable

# 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: https://keestalkstech.com/2021/03/share-aws-vault-on-wsl/
choco install -y awscli aws-vault
## docker, inspect docker files
choco install -y rancher-desktop dive
## IDE's
choco install -y vscode.install
# compare
choco install -y beyondcompare
# FTP
choco install -y filezilla
## git and git signing
## more info here: https://keestalkstech.com/2023/06/github-windows-ssh-gpg-devcontainer/
choco install -y git.install gpg4win
## testing APIs
choco install -y postman
## edit YAML files
choco install -y yq
## edit JS files
choco install -y jq
## 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 paint.net
## 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
## PowerShell Core
choco install -y powershell-core

# end

Enjoy!

Changelog

  • 2024-03-12: fixed the rancher-desktop, included dive.
expand_less