Getting Started with Chocolatey Package Manager

Installing and Getting Started with Chocolatey Package Manager for Windows

Getting Started with Chocolatey Package Manager

If you've ever got frustrated setting up the Windows development computer over and over again from scratch, there's an easier way. Perhaps you are testing and would like to install a particular set of applications on the system every time, and it's feeling tedious to do that manually. Let's see what is out there that can help you do that!

Chocolatey is a command-line package manager for Windows operating system based on the NuGet package manager. Chocolatey manages its own package feed; however, you can set up your own local repository for the enterprise environment to control the package's source for installation. If you are familiar with Linux or macOS environments, the Chocolatey is similar to Apt and Homebrew, respectively.

In this article, you'll learn how to install Chocolatey and then the usage of Chocolatey to install, uninstall, and update outdated packages using the command line.

Table of Content

  1. Prerequisites
  2. Installing Chocolatey on Windows
  3. Exploring Commands and Searching Chocolatey Package
  4. Installing Chocolatey Packages
  5. Using Chocolatey with PowerShell DSC
  6. Uninstalling Chocolatey Packages
  7. Conclusion

Prerequisites

  • Windows 10 / Windows Server 2012 or higher OS
  • PowerShell 5.1 or higher

Installing Chocolatey on Windows

You can install Chocolatey by either using the PowerShell script method or downloading the offline installer. Follow the steps below to download and install Chocolatey using the online method:

  • Open PowerShell in Administrator Mode by right-clicking on the application and choosing Run as Administrator:
https://s3-us-west-2.amazonaws.com/secure.notion-static.com/8c35ca6e-9941-4e5d-a532-9f2df662e185/01-powershell-admin.png
PowerShell Admin Mode
  • You need to update the script execution policy on your device before installing any package. Run the following command in the PowerShell terminal to allow script execution for the system:
Set-ExecutionPolicy Bypass -Scope Process
PowerShell Execution Policy
  • Type Y when prompted for confirmation and allow the changes:
https://s3-us-west-2.amazonaws.com/secure.notion-static.com/29ab4b8f-e391-4d73-94f0-17f29e08285f/02-policy-update.png
Execution Policy Change Confirmation
  • The next command does a few different things before installing chocolatey on the system. The first part of the command checks the security settings for the system, followed by downloading and running the install.ps1 that's downloaded from the chocolatey website without any additional dialog box. The install.ps1 PowerShell file contains the instructions to install and prepare the package manager for the system.
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('<https://chocolatey.org/install.ps1>'))
Installing Chocolatey using PowerShell
  • The output of the command will look like this:
https://s3-us-west-2.amazonaws.com/secure.notion-static.com/e3eaaeee-3b9e-49ff-8079-aea21189e799/03-choco-install.png
Chocolate Install Output
  • Verify the installation using the following command while in the same terminal session:
choco --help
Chocolatey Help
  • The output of the command will look like this, confirming the installation of chocolatey on the system:
https://s3-us-west-2.amazonaws.com/secure.notion-static.com/e05403e3-8690-444c-b0cf-640cab0fe811/04-choco-help.png
Chocolatey Help Output

Exploring Commands and Searching Chocolatey Package

Like most other PowerShell commands and tools, Chocolatey also comes with an inbuilt search and help command to make it easier to find the packages and get help for associated commands.

You can search for packages using the list, search, or info command. All these commands are an alias for each other and display similar results. The result for a package winrar looks like this:

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/801e1493-fe9a-4245-99e7-150d9443545e/05-choco-search.png
Chocolatey Search Winrar

The output displays the list of packages that matched the search. In this case, the output only displays Winrar 6.02. However, they may be more packages depending on the package name and similar packages release from different vendors. You can optionally search for winrar and observe the difference in the output.

Another useful command to know while using Chocolatey is the info command. Unlike the search command, this command gives out detailed information about the package and lists install instructions and any accepted parameters for the installation of the package. Below is an example of the Winrar package:

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/f4c96b30-530a-4eb0-bb56-8b1669727011/06-info.png
Chocolatey Info Winrar

The info command is very beneficial when identifying the details of what the package does and other relevant information about the package you will install.

Another common argument that you might often use when identifying any command's possible commands and arguments are the help argument. The  --help, /? or -? can be used to get assistance with almost any command.

Installing Chocolatey Packages

Once you have done enough research on the package you want to install, the next step is to download and install the package. The command used to install the chocolatey package is choco installfollowed by the name of the package of choice. The install command first downloads the package from the remote repository and then installs it on the operating system as part of one command.

The output of the command choco install Winrar will look like this:

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/66be044b-9584-4383-b21e-7fbc91a8bbc7/07-install-winrar.png
Chocolatey Install Winrar

You will be prompted to confirm the action if you have not specified the auto-accept parameters in the original command. You can add -y, --yes or --confirm at the end of your command to suppress the prompt in the terminal.

Installing multiple packages at once?
Use the following command while passing the name of the text file containing the list of packages to install:
Get-Content mypackage.txt | ForEach-Object {choco upgrade $_}

If you are planning to install a package but unsure about what exactly the package will do to install the application, you can add --whatif argument to the end of the statement to understand the process that will follow. Below is an example of whatif for Winrar installation:

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/33ca3172-6eff-47a6-bd51-9a5ea89bd14d/08-winrar-whatif.png
Chocolatey WhatIf

While installing a new package, you may have some old versions lying around on the system that you must update. Luckily, chocolatey has a outdated command that identifies any old packages installed on the system that can be updated. The following command list all the packages that are not using the latest version:

choco outdated
Chocolatey Outdated Command

If you identify any old package that you'd like to update, chocolatey comes with upgrade a command that updates one or multiple packages behind the latest version release. If the package is not installed on the system already, the upgrade command will prompt and install the application for you.

Run the following command and see the results in action:

choco upgrade googlechrome
Chocolatey Upgrade Command

Using Chocolatey with PowerShell DSC

You may likely need to use Chocolatey at the enterprise level or along with your DSC configurations if you are using PowerShell Desired State Configuration. I've covered the PowerShell DSC using Azure Automation account while using Chocolatey package installation as an example to accomplish the DSC state for windows machine at the link below:

PowerShell DSC in Azure: Zero to Hero
Infrastructure Compliance: A Definitive Guide to using PowerShell DSC with Azure Automation Account
PowerShell DSC in Azure Guide

Uninstalling Chocolatey Packages

Since you've seen multiple ways to install Chocolatey packages, the uninstall command only takes uninstall to remove any package from the system.

Run the following command to uninstall the Winrar package install earlier:

choco uninstall winrar
Chocolatey Uninstall Command

The output will look like this:

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/b3d69615-acc8-4f67-b0b0-c7dbb551f821/09-winrar-uninstall.png
Chocolatey Uninstall Winrar Output

Conclusion

I hope that gives you the context of the capabilities of Chocolatey and how you can use it to automate the setup of multiple computers using the Command Line tool with minimum human interference. Be sure to check the article below to see Chocolatey in action with Azure PowerShell DSC and check out other articles in the library.