Automating Windows Setup via Chocolatey
Tags: powershell, windows, automation, devops
Published: 2021-07-25 17:21:10

When you're a software professional, 1 thing you must've dreaded is setting up a new Windows machine (_as in a development environment_). This might be as the setup either needs downloading multiple .MSI / .EXE files of the required applications or using the Microsoft App Store to install (_personally I believe it's a little off the mark compared to the Linux/MacOS_).  

It's also compounded by the fact Windows systems are regarded relatively volatile so we never re-touch/re-configure applications once installed. You also must've surely wondered why Windows trails Linux/Mac OS when it comes to Command Line. Agreed, **[PowerShell](https://docs.microsoft.com/en-us/powershell)** changes most of that but learning a new syntax can be a little harrowing.

What if I told you this could be solved permanently via CLI? Enter Chocolatey to the rescue.

<img width="100%" style="width:100%" src="https://media.giphy.com/media/dW5tjSvxH1l94rDP06/giphy.gif">

Chocolatey is a package manager specifically designed to solve these very problems for Windows OS. Consider it equivalent to `apt`, `dnf` or `brew` that'll ease work considerably, so much that you might never go back to tedious manual methods again.


##Prerequisites:
+ Understanding how Powershell policies work such as `Set-ExecutionPolicy`, [here](https://adamtheautomator.com/set-executionpolicy) is an excellent page to get you started.
+ From the [Chocolatey official page](https://docs.chocolatey.org/en-us/choco/setup) 
  * Windows 7+ / Windows Server 2003+
  * PowerShell v2+ (_Not PowerShell Core, minimum is v3_)
  * .NET Framework 4+ (_installation attempts to install .NET 4.0 if absent, minimum is v4.5 due to TLS v1.2_)
+ Excitement to learn something new and preferably a cup of tea/coffee.


##Steps:
+ Follow [this link](https://docs.chocolatey.org/en-us/choco/setup#installing-chocolatey) to install Chocolatey.
+ If you get the below error, then install PowerShell v7.3 using [this](https://docs.microsoft.com/en-us/powershell/scripting/install/installing-powershell-core-on-windows?view=powershell-7.1#installing-the-msi-package) and then repeat the previous step.

![Get-Execution error](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cb8fuwghl23iticahj4w.png)

+ Once the install completes, you should be able to execute `choco --version`.

And that's all when you prefer the vanilla install but if you like to dive deeper, you'll quickly realize a few concerns.


- How can we install specific versions of software as needed?
- What if we wanted to install multiple applications in 1 go, uninterrupted?
- Can we rollout to multiple machines? (_Note: some features for this point may need Chocolatey enterprise, please read the documentation as per your use-cases_)

Lets answer the 1st two questions. 

Number 1 is pretty simple, you just execute `choco install vlc={version}` and relax. Number 2 can be a little bit tricky but a handy way is to use the [Packages.config](https://docs.chocolatey.org/en-us/choco/commands/install#packages.config) which is an XML file containing the list of applications we want to install. 

Now the command becomes `choco install {path-to-file.config} --yes`

[Here](https://github.com/vinayhegde1990/initial-workstation-config/tree/master/powershell/chocolatey) is a sample that you can use or create one/more referring it as per your requirements. You can also keep it under version control to have an audit for changes.


##Result?
On a fresh Windows 10 machine before this, it took me 2-3 hours to download & install the exes with atleast 70+ clicks (_mostly clicking Next on GUI installers_😛)

Now it took me ~35 minutes, which could be even lesser once we've the `.config` files ready as mentioned above.

Hope this guide helps you save time/effort similarly.

---

What are the ways you use to setup Windows environments?