Create a Backup of your Chocolatey Packages using Boxstarter

Create a Backup of your Chocolatey Packages using Boxstarter
Photo by Pushpak Dsilva / Unsplash

Introduction

Chocolatey is command line package manager for Windows that gives you a very Linux -esque software installation experience. This guide expects that you already are using Chocolatey, but in case you need convincing here's what makes it so awesome, for example: choco install google chrome will install Google Chrome on your computer without having to wait for the installer. You can even get fancy and list as many packages as you would like with a -y flag to automatically accept any prompts: choco install -y autohotkey.portable python3 gimp Can't undersell how easy this makes to set a computer up for the first time.

Boxstarter uses Chocolatey packages but adds a few extra tools that allow you to install software faster and make changes to Windows settings. Boxstarter has some amazing functionality that I am not going to touch on here, but I would recommend Checking out their Docs.

Prerequisites to make a Backup

The Process

1. Get a list of your Chocolatey Packages

Open Powershell and enter clist -l and copy the output into a file called data.txt Alternatively, clist -l > data.txt will automatically make a data.txt file in whatever directory Powershell is in.

2. Make a Python File with the following code:

import re

## Regex to get package names
regex = re.compile("(?:^|\\n)(\\S{1,})")

## Open data.txt
with open("data.txt", "r") as f:
    data = f.read()
    matchArray = regex.findall(str(data))

    ## Use regex to print package names to text file
    with open("boxstarterScript.txt", "w") as output:
        for match in matchArray:
            output.write("cinst " + match + "\n")

Then making sure data.txt is in the same folder, run the script. You should get boxstarterScript.txt as an output.

3. Add any Extra Commands

The code will output a file with a ton of lines that just say cinst python3 which will work perfectly fine by itself but we can leverage Boxstarter to do a few other cool things. I personally add the code below to the top of my Script. It enables a few Windows File Explorer, I think the flags are pretty self-explanatory, but if you want more details or want to see more flags check out the Boxstarter docs

Set-WindowsExplorerOptions -EnableShowHiddenFilesFoldersDrives -EnableShowProtectedOSFiles -EnableShowFileExtensions
Enable-RemoteDesktop

and at the end of the file, I add a command that forces windows to update.

Install-WindowsUpdate -AcceptEula

4. Using the Script.

Obviously this script that we made doesn't do any good if it is not accessible to a new machine so paraphrasing from the Boxstarter guide I'll give a quick rundown on how to quickly use your backup using gist. If you don't want to use Github, my understanding is that any public URL that gives a raw .txt page will work, or you can use a different method to get the .txt to your machine.

If you don't have Boxstarter or Chocolatey installed then open Powershell and run:

. { iwr -useb https://boxstarter.org/bootstrapper.ps1 } | iex; get-boxstarter -Force

Then you can run the script we made by opening the Boxstarter shell and running the Command below. Replace PATH_TO_boxstarterScript.txt with either the URL to your script. i.e. https://gist.githubusercontent.com/MisterBiggs/97e8e065f8a8d1d941a2c3377e229dad/raw/9804bbd5e3131ec9aebbff71395df9e382fbae1b/boxstarterScript.txt (Make sure you use the raw link to your gist) or C:\Users\albig\Documents\backupScript\boxstarterScript.txt

Install-BoxstarterPackage -PackageName PATH_TO_boxstarterScript.txt -DisableReboots

Conclusion

In this tutorial, we were able to make a backup of all the software installed on your machine in a way that is extremely easy to replicate and automate the install of on a new machine.