Automate PowerShell Modules with Chocolatey Packages

Published:1 August 2019 - 2 min. read

In this blog post, learn how to create Chocolatey package (a popular Windows packaging manager).

As automation becomes more of necessity rather than a nicety, it’s important we IT folk look at better ways to perform menial tasks; one of those tasks is installing software. Rather than download an MSI, double-click on it and run through the wizard, it would behoove you to think of a better way.

Not a reader? Watch this related video tutorial!
Not seeing the video? Make sure your ad blocker is disabled.

Nearly all software has command-line switches to install silently, and if you perform enough research, you might figure out the right switches. Your install might then look like this:

> install.exe /q /n /e /noreboot

Are you going to remember all of those switches? Probably not. You could put these switches in a batch file and then forget where you put that script or you could do this:

Install-Package -Name AcmeSoftware

Now that’s something that can be remembered easily.

But how would you get to that point? The answer is a NuGet repository and a NuGet package. NuGet is a traditional software packing technology that allows you to essentially wrap up a piece of software which can then be stored in a repository, downloaded and installed at will. Chocolatey is a favorite open source tool that uses NuGet to allow IT administrators to download and silently install software.

In this article, I’m going to assume you’ve already got a NuGet repository setup or you know where you’re going to send your package.

Let’s take a piece of software and wrap it up in a NuGet package for use in one of these repositories.

Installing Chocolatey

Since Chocolatey is not installed by default on a Windows operating system, it must be downloaded and installed. It can be downloaded and installed by running this in a PowerShell console:

PS> iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))

Creating an NUSPEC file

To create a Chocolatey package, you first must create an NUSPEC file. An NUSPEC file is an XML package manifest that describes the package contents. This NUSPEC file must be in a particular format. Here’s a sample NUSPEC file using only the required elements:

<?xml version="1.0" encoding="utf-8"?>
    <package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
      <metadata>
        <id>MySpecialSoftware</id>
        <version>1.0</version>
        <description>Some software I'm packaging up.</description>
        <authors>Adam Bertram</authors>
      </metadata>
    </package>

For the complete rundown of all the tags that can be used refer to the NUSPEC reference.

How to Create Chocolatey Package

Next, create the package. To do this, we use the Install-ChocolateyInstallPackage command. Let’s pretend that the software we’re packaging up is an EXE and the switches to install it silently are /s. We must send the file location along with the silent switches to use to the command.

PS> Install-ChocolateyPackage -PackageName 'AcmeSoftware' -FileType 'exe' -File 'C:\install.exe' -SilentArgs '/s'

This command will create the package. Next, we’ll need to pack the package using the choco exe.

PS> choco pack

Once the software has been packaged up, it’s now in a format to where you can publish to a Chocolatey repository of your choice.

Summary

This process can be repeated for each piece of software you need to deploy via Chocolatey. As you can see, there are only a few additional steps that must take place to get the software “Chocolatey-ready.” By doing so now allows you to get back to doing tasks that matter rather than figuring out how to deploy software to a bunch of machines at once!

If you’d like a deep dive more after you learned how to create Chocolatey packages, be sure to check out the Chocolatey docs.

Learning How to Create Chocolatey Package

Hate ads? Want to support the writer? Get many of our tutorials packaged as an ATA Guidebook.

Explore ATA Guidebooks

Looks like you're offline!