Skip to content

Instantly share code, notes, and snippets.

@lowleveldesign
Last active November 5, 2023 11:41
Show Gist options
  • Star 25 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save lowleveldesign/7977df3113312e589035 to your computer and use it in GitHub Desktop.
Save lowleveldesign/7977df3113312e589035 to your computer and use it in GitHub Desktop.
Scripts to automatically update all Chocolatey packages installed on your system

Below you can find two scripts which will update all Chocolatey packages installed on your system. The main script is UpdateChoclatey.ps1, but for convenience I also add UpdateChocolatey.bat which will call the former Powershell script (place it in the same folder as the .ps1 file).

The script asks for elevation if needed.

Example call:

C:\> UpdateChocolatey.bat

========= Updating chocolatey... =========
Updating 7zip...
Chocolatey v0.9.9.6
Upgrading the following packages:
7zip
By upgrading you accept licenses for the packages.
7zip v15.14 is the latest version available based on your source(s).

Chocolatey upgraded 0/1 package(s). 0 package(s) failed.
 See the log for details (C:\ProgramData\chocolatey\logs\chocolatey.log).
Updating 7zip.install...
Chocolatey v0.9.9.6
Upgrading the following packages:
7zip.install
By upgrading you accept licenses for the packages.
7zip.install v15.14 is the latest version available based on your source(s).

...

Press any key to continue...
@echo off
powershell -NoProfile -ExecutionPolicy ByPass -File "%~d0%~p0%~n0.ps1"
$here = Split-Path -parent $MyInvocation.MyCommand.Definition
$script = $MyInvocation.MyCommand.Name
$identity = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$principal = New-Object System.Security.Principal.WindowsPrincipal($identity)
if (-not $principal.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator)) {
Write-Warning "Not running with administrative rights. Attempting to elevate..."
$command = "-ExecutionPolicy bypass -File `"$here\$script`""
Start-Process powershell -verb runas -argumentlist $command
Exit
}
Write-Host "`n========= Updating chocolatey... ========="
& choco upgrade -y all
Write-Host "Press any key to continue..."
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
@abrookewood
Copy link

Not sure if this is necessary anymore? Looks like you can just call Chocolatey to do it: choco upgrade all -y

@lowleveldesign
Copy link
Author

@abrookewood , look at the line 14 - this script calls upgrade all -y. The rest is code to relaunch the script as Administrator if it is run by a regular user.

@abrookewood
Copy link

Ahhh ... where is the facepalm emoji when you need it?

@lowleveldesign
Copy link
Author

Haha, no worries. This script used to be much longer before I discovered upgrade all -y, so I understand the confusion 🙂

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