Categories
Tips

Chocolatey Script Dev. Machine Setup

What is Chocolatey? How it makes developers lives easier?

Chocolatey is a package manager for Windows OS. much like Nuget Package Manager or Note Package Manager. You can use it to create scripts for un-attended software installations. We will walk through how to create such a script.

Run Powershell in Administrative mode. Create a file called choco-script.ps1 in d:\. (You can create it anywhere you want however though.)

Add the following piece of script in the file:

$testchoco = powershell choco -v
if(-not($testchoco)){
    Write-Output "Seems Chocolatey is not installed, installing now"
    Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
}
else{
    Write-Output "Chocolatey Version $testchoco is already installed"
}

It is checking if the chocolatey is installed or not and if it is not installed it will go ahead and install it for you. Next we are going to add some packages to the file.

Write-Host "Installing google chrome..." -ForegroundColor White -BackgroundColor Black
choco install googlechrome -y

Write-Host "Installing git..." -ForegroundColor White -BackgroundColor Black
choco install git.install -y

Let’s look at these two packages, we are installing Google Chrome and Git. We are passing in -y flag as by default chocolatey asks us to accept the individual package terms and conditions. By passing -y we are letting choco know that we accept it. Hence the script won’t stop and wait for our input. We can pass in -v, –verbose flags to see every message, on the contrary pass in -r to restrict or limit the output to essential information only. Complete list of flags can be found in the docs here.

Let’s add a few more packages and the file should look like this:


$testchoco = powershell choco -v
if(-not($testchoco)){
    Write-Output "Seems Chocolatey is not installed, installing now"
    Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
}
else{
    Write-Output "Chocolatey Version $testchoco is already installed"
}

Write-Host "Installing google chrome..." -ForegroundColor White -BackgroundColor Black
choco install googlechrome -y

Write-Host "Installing node version manager..." -ForegroundColor White -BackgroundColor Black
choco install nvm -y


Write-Host "Installing git..." -ForegroundColor White -BackgroundColor Black
choco install git.install -y
Write-Host "Installing git-lfs..." -ForegroundColor White -BackgroundColor Black
choco install git-lfs.install -y
Write-Host "Installing poshgit..." -ForegroundColor White -BackgroundColor Black
choco install poshgit -y

Write-Host "Installing notepad++..." -ForegroundColor White -BackgroundColor Black
choco install notepadplusplus.install -y

Write-Host "Installing postman..." -ForegroundColor White -BackgroundColor Black
choco install postman -y
Write-Host "Installing fiddler..." -ForegroundColor White -BackgroundColor Black
choco install fiddler -y
Write-Host "Installing putty..." -ForegroundColor White -BackgroundColor Black
choco install putty -y
Write-Host "Installing agent ransack..." -ForegroundColor White -BackgroundColor Black
choco install agentransack -y

I didn’t include all the packages that i use, above list is just to give you an idea, you can create your own list of favorite tools. You can find all available packages on chocolatey.org.

You can also install windows features using choco, to view the list of windows features run:

> choco list --source windowsFeatures

Chocolatey v0.10.15
Deployment Image Servicing and Management tool
Version: 10.0.19041.572
Image Version: 10.0.19041.572
Features listing for package : Microsoft-Windows-Foundation-
------------------------------------------- | --------
Feature Name                                | State
------------------------------------------- | --------
Printing-PrintToPDFServices-Features        | Enabled
Printing-XPSServices-Features               | Enabled
SearchEngine-Client-Package                 | Enabled
MSRDC-Infrastructure                        | Enabled
TelnetClient                                | Disabled
TFTP                                        | Disabled
TIFFIFilter                                 | Disabled
...

The syntax slightly changes as we provide the source of the installation, here are two examples:

Write-Host "Installing Installing Microsoft-Hyper-V..." -ForegroundColor White -BackgroundColor Black
choco install Microsoft-Hyper-V -y -source windowsfeatures

Write-Host "Installing Installing NetFx4Extended-ASPNET45..." -ForegroundColor White -BackgroundColor Black
choco install NetFx4Extended-ASPNET45 -y -source windowsfeatures

That’s all folks, happy coding.