Automate boring installations with Chocolatey
New windows installation
Whenever we need to setup a new machine its always a time consuming & boring process. Being a developer you need to know when to automate the boring stuff.
“I choose a lazy person to do a hard job. Because a lazy person will find an easy way to do it.” - Bill Gates.
If you are running a third party script from the internet, it’s always scary and we feel insecure that it will harm our computer. But how about you write each and every single line on your own and run it ? Its not that hard to automate these stuffs.
Linux users have inbuilt package managers. But for windows microsoft is working on something called
winget, but it might take several moths to be fully polished and famous. Don’t worry there’s a another famous package manager called
Chocolatey has more than 7.5K stars on along with excellent community support.
Why chocolatey ?
Imagine you have a fresh windows pc now. Think about all the applications you will need. The first and foremost step that most of us do is to install
google chrome. I’ve done this myself using edge a well over dozen times. It’s 2021. We shouldn’t repeat things and let the machine take care of it.
Photo by Alex Knight on Unsplash
This will be the first step. Install the official chocolatey package manager so that it can do the boring stuff. You can also automate installation of this as well. At the beginning of your script you will have to add
@powershell -NoProfile -ExecutionPolicy Bypass -Command "iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))" && SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin"
Here we are using the
Invoke-Expression commands alias
iex and using the
WebClient to install the package manager from the chocolatey site and updating the executable in our user profile’s bin path.
Now I assume before you format your existing machine or even if you have a new laptop you know the set of applications that you will be using and that are to be installed. If you have a list of them it’s well and good. But if you don’t have a ready made list you can always just use the [Chocolatey Packages] ( https://community.chocolatey.org/packages) page to find your application.
Then all you need to do is just add the below statement along with your
An example to install chrome
choco install googlechrome
If you need to install multiple applications just repeat the
choco install command along with your application name. Whenever you run the command chocolatey asks for user confirmation that whether you agree to install this from source X. In order to overcome this when installing multiple packages together you can just use the below statement at the top of your script.
choco feature enable -n allowGlobalConfirmation
You can combine print statements using
Write-Host if you are using powershell or
echo statements for bash scripts to pretty the script and give feedback to the user when running through the script. Eg something like this
echo This will first install chocolatey, then other tools echo . echo Browse https://chocolatey.org/packages for packages echo . echo Ensure that your cmd.exe runs as Administrator echo . powershell -NoProfile -ExecutionPolicy Bypass -Command "iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))" && SET PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin choco feature enable -n=allowGlobalConfirmation echo Now chocolatey should be ready and we can go ahead echo . #Disabling user confirmation choco feature disable -n allowGlobalConfirmation #Browsers choco install googlechrome choco install microsoft-edge #Essentials choco install microsoft-teams choco install nvidia-display-driver #DevTools choco install vscode choco install microsoft-windows-terminal choco install notepadplusplus.install choco install git choco install fiddler4 choco install nodejs-lts choco install github choco install mongodb choco install postman choco install visualstudio-installer ##SDK choco install dotnetcore-sdk #Misc choco install 7zip choco install spotify choco install notion #Terminal choco install microsoft-windows-terminal choco install oh-my-posh choco install poshgit
Once everything is done, you will have all your applications ready to be used. You can also commit this script on your Github gist and share it with your friends. I hope this blog helped you in automating a few things.