DEV Community

loading...
Cover image for Automating Windows Setup via Chocolatey

Automating Windows Setup via Chocolatey

Vinay Hegde
Site Reliability Engineer with 6+ years of experience. Otherwise an avid artist, reader, cinephile & football fan.Looking forward to connecting with everyone :)
Updated on ・3 min read

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 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.

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 is an excellent page to get you started.
  • From the Chocolatey official page
    • 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 to install Chocolatey.
  • If you get the below error, then install PowerShell v7.3 using this and then repeat the previous step.

Get-Execution error

  • 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 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 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?

Discussion (26)

Collapse
djibe profile image
djibe

Hi,
I did the same performance with Winget recently.
And Winstall (winstall.app/) is awesome for this.

Collapse
jonjpbm profile image
jonjpbm

Also

"The winget tool requires Windows 10, version 1809 (10.0.17763), or a later version of Windows 10."

Which might be limiting.

Collapse
sharpninja profile image
The Sharp Ninja

Why? That barely a supported version. Anyone on an older cersion is at risk.

Thread Thread
vinayhegde1990 profile image
Vinay Hegde Author

@sharpninja what exactly do you mean by this?

Thread Thread
sharpninja profile image
The Sharp Ninja

Security patches. You should always use a supported version of software products.

Thread Thread
vinayhegde1990 profile image
Vinay Hegde Author

I agree, but from observation people don't do it proactively for Windows

Thread Thread
sharpninja profile image
The Sharp Ninja

And that's a real problem! People don't want to be bothered to learn about TPM 2.0 because if they did bother, they would learn the vast majority of computers already have it, and the ones that don't just need an add-in card. Instead, they would rather remain insecure so they can whine about the Win 11 min reqs.

Thread Thread
vinayhegde1990 profile image
Vinay Hegde Author • Edited

That's because of a handshake between Microsoft's arcane documentation and burnt child dreads the fire experiences people have had when tinkering with Windows until now. But it does seem Windows 11 will change that perception for the better.

Thread Thread
sharpninja profile image
The Sharp Ninja

I don't see the connection between using an open standard used by both Windows and Linux and blaming the fact that in the past people did stupid things to justify being intentionally uninformed and resistant to the same techniques that Apple and Linux also use.

Thread Thread
vinayhegde1990 profile image
Vinay Hegde Author

When it's Windows, people aren't intentionally uninformed but rather due to unawareness or little/no choice/lack of alternatives.

Thread Thread
sharpninja profile image
The Sharp Ninja

Please explain, I am not following your logic.

Thread Thread
vinayhegde1990 profile image
Vinay Hegde Author

Please re-read my previous comments, I already have.

Thread Thread
sharpninja profile image
The Sharp Ninja

No, you use circular logic to say that people are uninformed because they have been burned but alsi claim that people aren't wilfully ignoring facts. But when people choose to ignore facts because they have been burned before they are still willfully ignoring facts.

Thread Thread
vinayhegde1990 profile image
Vinay Hegde Author • Edited

Unsure how you interpeted circular logic out of those 2 comments, let me make it easier

Tweaking Windows to one's preferences is difficult as Microsoft doesn't permit changes to the OS or any components in it. They also have a history of botched OTA updates.

This then leads people to stay away from customisations & ignore future updates willfully as they don't want to tamper a working environment & impact their daily work.

Collapse
jonjpbm profile image
jonjpbm

"The winget tool is currently a preview, so not all planned functionality is available at this time."

docs.microsoft.com/en-us/windows/p...

Collapse
vinayhegde1990 profile image
Vinay Hegde Author

Yes I remember winget isn't live yet so it won't be usable for all. Also chocolatey is relatively evolved and supports more functions along with a super helpful community.

Thanks for the ideas @jonjpbm and @djibe

Collapse
jonrandy profile image
Jon Randy

The steps I usually use are:

1) Download the iso image for Ubuntu
2) Make a bootable USB drive
3) Wipe Windows with Ubuntu
4) Proceed from there

Collapse
vinayhegde1990 profile image
Vinay Hegde Author • Edited

@jonrandy +1 if that's sarcasm but you could lessen effort even further by not installing Windows at all

Collapse
jonrandy profile image
Jon Randy

I didn't. Damn near impossible to get a machine with Ubuntu pre-installed here in Thailand

Thread Thread
vinayhegde1990 profile image
Vinay Hegde Author

Hard luck there! It can be skipped if an assembled PC is available but a laptop will surely come with Windows pre-installed so now I know why step 3 is needed.

Collapse
sharpninja profile image
The Sharp Ninja

Using the FreshBuild for PowerShell you can automate Choco, Scoop and Winget from a single config file that you can use from a Gist.

Collapse
vinayhegde1990 profile image
Vinay Hegde Author

Looks interesting but any specific reason one should prefer Freshbuild over any of the above installers?

Collapse
sharpninja profile image
The Sharp Ninja

Freshbuild can install all three repo managers at once in addition to executing them in a set sequence.

Thread Thread
vinayhegde1990 profile image
Vinay Hegde Author

Useful feature but multiple managers could mess up app installs or cause interference with one another.

Thread Thread
sharpninja profile image
The Sharp Ninja

True, but not all apps are in more than one of these repositories, especially the stuff in scoop.

Thread Thread
vinayhegde1990 profile image
Vinay Hegde Author • Edited

Scoop has more dev tools + an extras bucket for non-dev stuff (Skype, Slack etc..) but some apps are either missing or a bit dated.

Overall very promising, thanks for the info