Getting Started with the Chocolatey Package Manager

Francisco Navarro

Francisco Navarro

Read more posts by this author.

If you’re struggling with managing and maintaining software across your environment, you’re in for a treat. In this article, you’re going to learn all about the ubiquitous package management tool called Chocolatey. Whether you’re building a DevOps pipeline, attempting to automate software deployments or an individual user wanted to speed up software installations, this article is for you.

In this article, you’ll learn, hands-on, how to install the Chocolatey client, manage software and get a basic introduction to what makes this product tick.

Chocolatey: Package Management Came to Windows

Package management that allows administrators of all kinds to standardize, automate and manage software. This concept has been around in the Linux world for a long time but hasn’t, until recently, gotten a lot of traction on Windows.

If you’re new to the concept of Windows package management, you are highly encouraged to check out the blog post Understanding Package Management on Windows.

Package management on Windows is dependent on a technology called NuGet. NuGet is a technology that allows developers and administrators to create, share, and consume useful code and software packages. NuGet provides similar advantages that of which resemble package management on Linux.

The Rise of Chocolatey

Plain NuGet had its challenges though. It was primarily built with the software developer in mind. There weren’t many tools out there to bring this package management technology to other engineers and IT administrators…until Chocolatey came along.

Chocolatey was built as a framework around NuGet that was and still is popular with the Microsoft community. It was so popular that it caught Microsoft’s attention.

When PowerShell v5 was released, it supported Chocolatey through its own technology called  PowerShell OneGet. This native integration along with popular community support has now made Chocolatey the de facto standard for package management on Windows.

If you’d like to learn about Chocolatey, you’re encouraged to check out the various learning resources they provide on their website.

Prerequisites

The rest of this article is going to be a walkthrough. If you’ve never set up Chocolatey before, you’re going to learn how to get started from scratch. If you plan to follow along, please be sure you have the following prerequisites fulfilled.

  • Windows 10 – Windows 10 1903 will meet all the technical requirements out of the box. Although this isn’t technically required by Chocolatey, it is what all examples in this article will use.
  • Windows PowerShell 5.1 – Although this isn’t technically required by Chocolatey, it is what all examples in this article will use.
  • The ability to launch an elevated Windows command prompt and PowerShell console

For a full breakdown of all technical requirements, check out the Chocolatey Requirements page.

Installing the Chocolatey Client

When getting started with Chocolatey, your first task will be downloading and installing the Chocolatey client. Technically, Chocolatey has a server component as well but for this article, you’ll be focusing on using the client to download, install and update software.

There are multiple ways of installing the Chocolatey client, but in this article, you’re going to learn the most common way, using PowerShell. For a detailed breakdown of installing the Chocolatey client in many different ways, check out the Chocolatey installation guide.

Setting the PowerShell Execution Policy

Chocolatey provides a PowerShell script to execute on your system. Because it is, a PowerShell script and must run on your system, you must first ensure the PowerShell execution policy is set appropriately to allow the script to run.

First, to ensure you need to change the execution policy in the first place, open up an elevated PowerShell console and run Get-ExecutionPolicy. You’ll need to ensure the Process execution policy is set to Unrestricted or Bypass. You’re changing the Process scope here because it is temporary. Once you close the PowerShell console, the execution policy will revert back.

Below you  can see that the Process scope is currently Restricted. This needs to be changed.

PS51> Get-ExecutionPolicy -List
Scope         ExecutionPolicy
-----         ---------------
MachinePolicy Undefined
UserPolicy    Undefined
Process       Restricted
CurrentUser   Restricted
LocalMachine  Restricted

To change the execution policy, use the Set-ExecutionPolicy cmdlet providing the scope to change and the execution policy as shown below.

PS51> Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass 

Execution Policy Change.   

The execution policy helps protect you from scripts that you do not trust.  

Changing the execution policy might expose you to the security risks described in 
the about_Execution_Policies help topic at https:/go.microsoft.com/fwlink/?LinkID=135170.

## Select "Yes to All" or A.
Do you want to change the execution policy 
[*Y*] Yes [*A*] Yes to All [*N*] No [*L*] No to All [*S*] Suspend [?] Help (default is "N"): A

Do not close the PowerShell console. If you do, you will lose your execution policy changes. Remember that the execution policy change is meant to be temporary and only for the session you set it on.

Running the Installation Script

Chocolatey provides a handy PowerShell script to install the client. Once the PowerShell execution policy has been set appropriately, you can now run the script. Before you do though, it’s always a good idea to inspect a script before you download and run it. If you’d like to see what this script looks like, you can view the install.ps1 script it in its entirety here.

Once you’re satisfied with what it does, you can can now download and execute it all in one command. Chocolatey documentation uses the Invoke-Expression cmdlet to download and execute the script from the https://chocolatey.org/install.ps1 URL. Once this download and begins running, you will then see the various output as shown below.

PS51> Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('<https://chocolatey.org/install.ps1>'))

Getting latest version of the Chocolatey package for download.
Getting Chocolatey from <https://chocolatey.org/api/v2/package/chocolatey/0.10.15>.
Downloading 7-Zip commandline tool prior to extraction.
Extracting C:\\Users\\Administrator\\AppData\\Local\\Temp\\chocolatey\\chocInstall\\chocolatey.zip to C:\\Users\\Administrator\\AppData\\Local\\Temp\\chocolatey\\chocInstall...
Installing chocolatey on this machine
Creating ChocolateyInstall as an environment variable (targeting 'Machine')
Setting ChocolateyInstall to 'C:\\ProgramData\\chocolatey'
WARNING: It's very likely you will need to close and reopen your shell
  before you can use choco.
Restricting write permissions to Administrators
We are setting up the Chocolatey package repository.
The packages themselves go to 'C:\\ProgramData\\chocolatey\\lib'
  (i.e. C:\\ProgramData\\chocolatey\\lib\\yourPackageName).
A shim file for the command line goes to 'C:\\ProgramData\\chocolatey\\bin'
  and points to an executable in 'C:\\ProgramData\\chocolatey\\lib\\yourPackageName'.
 
Creating Chocolatey folders if they do not already exist.
 
WARNING: You can safely ignore errors related to missing log files when
  upgrading from a version of Chocolatey less than 0.9.9.
  'Batch file could not be found' is also safe to ignore.
  'The system cannot find the file specified' - also safe.
WARNING: Not setting tab completion: Profile file does not exist at 'C:\\Users\\Administrator\\Documents\\WindowsPowerShell\\Microsoft.PowerShell_profile.ps1'.
Chocolatey (choco.exe) is now ready.
You can call choco from anywhere, command line or powershell by typing choco.
Run choco /? for a list of functions.
You may need to shut down and restart powershell and/or consoles
 first prior to using choco.
Ensuring chocolatey commands are on the path
Ensuring chocolatey.nupkg is in the lib folder~~

Verifying Installation

Once the PowerShell script has finished and you are returned back to the PowerShell console, it’s a good idea to confirm you can run the Chocolatey client called choco.exe.

When you run choco, you should see the installed version and some basic help information as shown below.

PS51> choco
Chocolatey v0.10.15
Please run 'choco -?' or 'choco <command> -?' for help menu.~~

Getting Help

One of the best things about the Chocolatey CLI client is its help system. The built-in help system does an excellent job of documenting all the functionality of Chocolatey. To access the help system, provide the -? switch to the choco command as shown below.

You can see the Chocolatey client has various commands where you can perform many different functions to manage software.

PS51> choco -?
This is a listing of all of the different things you can pass to choco.
 
Commands
 
 * list - lists remote or local packages
 * find - searches remote or local packages (alias for search)
 * search - searches remote or local packages (alias for list)
 * info - retrieves package information. Shorthand for choco search pkgname --exact --verbose
 * install - installs packages from various sources
 * pin - suppress upgrades for a package
 * outdated - retrieves packages that are outdated. Similar to upgrade all --noop
 * upgrade - upgrades packages from various sources
 * uninstall - uninstalls a package
 * pack - packages up a nuspec to a compiled nupkg
 * push - pushes a compiled nupkg
 * new - generates files necessary for a chocolatey package from a template
 * sources - view and configure default sources (alias for source)
 * source - view and configure default sources
 * config - Retrieve and configure config file settings
 * feature - view and configure choco features
 * features - view and configure choco features (alias for feature)
 * setapikey - retrieves, saves or deletes an apikey for a particular source (alias for apikey)
 * apikey - retrieves, saves or deletes an apikey for a particular source
 * unpackself - have chocolatey set itself up
 * version - [DEPRECATED] will be removed in v1 - use `choco outdated` or `cup <pkg|all> -whatif` instead
 * update - [DEPRECATED] RESERVED for future use (you are looking for upgrade, these are not the droids you are looking for)
 
 
Please run chocolatey with `choco command -help` for specific help on
 each command.

If you’re ever stuck trying to figure out how to perform a specific action, check out the help system.

Finding Packages

Once you’ve got the Chocolatey client installed and running, it’s time to see what kind of packages are available to install. The client has three commands that allow you to do just that called list, search, and find. These commands scour a Chocolatey repository for available packages or to list packages on your machine.

Below you will find the breakdown and examples found in the help system.

PS51> choco list --help

List/Search Command
 
Chocolatey will perform a search for a package local or remote.
 
* choco list - lists remote or local packages
* choco search - searches remote or local packages (alias for list)
* choco find - searches remote or local packages (alias for search)
 
Usage:
 
choco search <filter> [<options/switches>]
choco list <filter> [<options/switches>]
clist <filter> [<options/switches>]
 
Popular Switch --localonly or -lo
Usage of -lo:
 
choco <search,list,find> --localonly

As an example, here are the packages that are installed on my computer.

PS51> choco list -lo
Chocolatey v0.10.15
chocolatey 0.10.15
chocolatey-core.extension 1.3.3
GoogleChrome 77.0.3865.90
3 packages installed.

Inspecting Detailed Information about Packages

It’s a good practice to understand the intricacies of any package you plan on installing on a machine. The info command provides the ability to reference details of the package such as Package Author, Number of Downloads, Checksum, Source, and Software Site, among others.

Below you will find the breakdown and examples found in the help system.

PS51> choco info --help

Info Command
 
Chocolatey will perform a search for a package local or remote and provide
detailed information about that package. This is a synonym for
`choco search <pkgname> --exact --detailed`.
 
* choco info pkgname - retrieves package information.
* Shorthand for choco search pkgname --exact --verbose
* Shorthand for choco search pkgname --exact --detailed
 
 
Usage:
 
choco info [<options/switches>]

As an example, you can see below the information returned by querying the googlechrome package on a remote Chocolatey repository.

PS51> choco info googlechrome
Chocolatey v0.10.15
GoogleChrome 77.0.3865.90 [Approved] Downloads cached for licensed users
Title: Google Chrome | Published: 9/18/2019
Package approved as a trusted package on Sep 18 2019 20:15:10.
Package testing status: Passing on Sep 18 2019 18:45:17.
Number of Downloads: 19851699 | Downloads for this version: 262131
Package url
Chocolatey Package Source: <https://github.com/chocolatey-community/chocolatey-coreteampackages/tree/master/automatic/googlechrome>
Package Checksum: 'v7bDDUTo21k31xqUW+1amzaEWjeOqsEgS3txYNRgRaO3Q7ZnkzYz4S69AxTFn4r0HUB5Wi7NHXZBag/OaJTPKA==' (SHA512)
Tags: google chrome web internet browser admin
Software Site: <https://www.google.com/chrome/browser/>
Software License: <https://www.google.it/intl/en/chrome/browser/privacy/eula_text.html>
Description: Chrome is a fast, simple, and secure web browser, built for the modern web.

Installing Packages

Enough getting information about packages. It’s time to actually get some software installed! To do so, you’ll use the install command. This command is mighty and essential. If there is one choco command you should remember and become a pro at, it’s this one.

The install command installs a package or a list of packages you provide once you know what you need.

Below you will find the breakdown and examples found in the help system.

PS51> choco install --help

Install Command
 
Installs a package or a list of packages
 
Usage:
 
choco install <pkg|packages.config> [<pkg2> <pkgN>] [<options/switches>]
	cinst <pkg|packages.config> [<pkg2> <pkgN>] [<options/switches>]
 
Examples:
 
choco install sysinternals
choco install notepadplusplus googlechrome atom 7zip
choco install notepadplusplus --force --force-dependencies
choco install notepadplusplus googlechrome atom 7zip -dvfy
choco install git -y --params="'/GitAndUnixToolsOnPath /NoAutoCrlf'"
choco install git -y --params="'/GitAndUnixToolsOnPath /NoAutoCrlf'" --install-arguments="'/DIR=C:\\git'"
	# Params are package parameters, passed to the package
	# Install args are installer arguments, appended to the silentArgs
	#  in the package for the installer itself
choco install nodejs.install --version 0.10.35
choco install git -s "'<https://somewhere/out/there>'"
choco install git -s "'<https://somewhere/protected>'" -u user -p pass

Keeping Packages Updated

Once a package is installed, you will undoubtedly want to keep it up to date. This is a powerful feature of Chocolatey.

Upgrading Packages

So you want to keep your applications up to date? You will quickly find that choco doesn’t run automatically in the background. The Chocolatey client doesn’t run as a service. You have to invoke choco manually every time you need to upgrade your package(s). To do so, you’ll run the choco command upgrade along with listing the package(s) you want to update.

You can upgrade all packages to the latest version by running choco upgrade all.

Below you will find the breakdown and examples found in the help system.

PS51> choco upgrade --help

Upgrade Command
 
Upgrades a package or a list of packages. Some may prefer to use `cup`
 as a shortcut for `choco upgrade`. If you do not have a package
installed, upgrade will install it.
 
 
Usage:
choco upgrade <pkg | all> [<pkg2> <pkgN>] [<options/switches>]
cup <pkg | all> [<pkg2> <pkgN>] [<options/switches>]
 
Examples:
 
choco upgrade chocolatey
choco upgrade notepadplusplus googlechrome atom 7zip
choco upgrade notepadplusplus googlechrome atom 7zip -dvfy
choco upgrade git -y --params="'/GitAndUnixToolsOnPath /NoAutoCrlf'"
choco upgrade git -y --params="'/GitAndUnixToolsOnPath /NoAutoCrlf'" --install-args="'/DIR=C:\\git'"
# Params are package parameters, passed to the package
# Install args are installer arguments, appended to the silentArgs
#  in the package for the installer itself
choco upgrade nodejs.install --version 0.10.35
choco upgrade git -s "'<https://somewhere/out/there>'"
choco upgrade git -s "'<https://somewhere/protected>'" -u user -p pass
choco upgrade all
choco upgrade all --except="'skype,conemu'"

The upgrade command will install the package if the package doesn’t exist on the system.

Finding Local, Outdated Packages

What if you want to check if there is a new version of your package before you blindly run choco upgrade pkgname? You can use the outdated command. The outdated command scans through your installed packages and cross-references your designated package repository for potential package updates.

Below you will find the breakdown and examples found in the help system.

PS51> choco outdated --help

Outdated Command
 
Returns a list of outdated packages.
 
* outdated - retrieves packages that are outdated. Similar to upgrade all --noop
 
Usage:
 
choco outdated [<options/switches>]
 
Examples:
 
choco outdated
choco outdated -s <https://somewhere/out/there>
choco outdated -s "'<https://somewhere/protected>'" -u user -p pass

As an example, you can see below that all installed packages on my system are up to date.

PS51> choco outdated
Chocolatey v0.10.15
Outdated Packages
Output is package name | current version | available version | pinned?
 
 
Chocolatey has determined 0 package(s) are outdated.

Suppressing Package Updates

Keeping software up to date is definitely recommended but what if you have packages where you need to explicitly prevent that? Why would you want to do that?

One reason for suppressing updates would be to avoid update redundancy. For example, perhaps you may have just been looking to automate the initial install of a package. You don’t want to maintain the package’s entire software lifecycle.

Recall that by using the command choco upgrade all will update all local packages. Perhaps you need to make some exclusions from that command updating certain packages. In that case, you can pin packages.

When you run various Chocolatey commands, you will see a pinned attribute. For example, when running the outdated command, you can clearly see that attribute as shown below.

PS51> choco outdated
Chocolatey v0.10.15
Outdated Packages
Output is package name | current version | available version | pinned?

Below you will find the breakdown and examples found in the help system

PS51> choco pin --help

Pin Command
 
Pin a package to suppress upgrades.
 
This is especially helpful when running `choco upgrade` for all
 packages, as it will automatically skip those packages. Another
 alternative is `choco upgrade --except="pkg1,pk2"`.
 
Usage
 
	choco pin [list]|add|remove [<options/switches>]
 
Examples
 
	choco pin
	choco pin list
	choco pin add -n=git
	choco pin add -n=git --version 1.2.3
	choco pin remove --name git
* pin - suppress upgrades for a package

Uninstalling Packages

Finally, perhaps you’ve decided you no longer need software installed. Rather than manually going into Control Panel, finding the software and removing it that way, use the uninstall command to make quick work of it.

The uninstall command can remove one or many software packages all in one swoop. The uninstall command uninstalls a package or list of packages from a machine.

Below you will find the breakdown and examples found in the help system.

PS51> choco uninstall --help

Uninstall Command
 
Uninstalls a package or a list of packages.
 
Usage:
 
choco uninstall <pkg | all> [pkg2 pkgN] [options/switches]
cuninst <pkg | all> [pkg2 pkgN] [options/switches]
 
NOTE: `all` is a special package keyword that will allow you to
uninstall all packages.
 
Examples:
 
choco uninstall git
choco uninstall notepadplusplus googlechrome atom 7zip
choco uninstall notepadplusplus googlechrome atom 7zip -dv
choco uninstall ruby --version 1.8.7.37402
choco uninstall nodejs.install --all-versions

Playing it Safe Before Making Changes

Automating software management can be a destructive task if not performed properly. Rather than making a lot of changes at once, it’d be smarter to first run a test to see what would have happened. Luckily, the Chocolatey client has a few parameters to do just that.

The WhatIf Switch

Every choco command has a switch called --whatif. Alternatively, you can also use  --noop or--what-if. If these switches are used with a choco command, Chocolatey won’t actually perform the action but inform you what would have happened. You can see an example below attempting to uninstall all packages on the local system.

PS51> choco uninstall all -whatif

Chocolatey v0.10.15
_ Chocolatey:ChocolateyUninstallCommand - Noop Mode _
Would have uninstalled chocolatey v0.10.15.
Would have uninstalled GoogleChrome v77.0.3865.90.
Would have uninstalled chocolatey-core.extension v1.3.3.

Confirming Actions

When you perform a choco install package-name you’re prompted to confirm if you want the installation process to continue. Choco also requires confirmation for additional packages that get called as a dependency for your initial package.

You can see below an example of how Chocolatey first prompts to install Firefox before actually performing the action.

PS51> choco install firefox
Chocolatey v0.10.15
Installing the following packages:
firefox
By installing you accept licenses for the packages.
Progress: Downloading Firefox 69.0.1... 100%
 
Firefox v69.0.1 [Approved]
firefox package files install completed. Performing other installation steps.
The package Firefox wants to run 'chocolateyInstall.ps1'.
Note: If you don't run this script, the installation will fail.
Note: To confirm automatically next time, use '-y' or consider:
choco feature enable -n allowGlobalConfirmation
Do you want to run the script?([Y]es/[A]ll - yes to all/[N]o/[P]rint):

The confirmation process is a failsafe, but it can slow you down. If you want the process to continue uninterrupted and be unattended you must add a switch of -y, --yes or --confirm at the end of your command.

Using the -y switch works well when running commands unattended such as in a scheduled task.

Summary

In this article, you learned, at a high level, what Chocolatey is, how to get it set up and how it works. You learned how to use Chocolatey and some of the most commonly used commands to get started managing software via packages on Windows.

I encourage you to continue your journey with Windows package management and Chocolatey.

Make your workflow and life sweeter with Chocolatey!

Further Reading

Subscribe to Adam the Automator

Get the latest posts delivered right to your inbox

Looks like you're offline!