PowerShell script to install Chocolatey and a list of packages

Benjamin Cuningham picture Benjamin Cuningham · Jan 8, 2018 · Viewed 21.1k times · Source

My goal is to create a single script that I can download and run on a fresh Windows build to set up the system as much as possible. The first thing I am trying to do is install as many of the programs that I always like to have available as possible. I previously ran this (it is from chocolatey.org) to install Chocolatey directly from PowerShell:

Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))

Then I ran a bunch of choco installs like this:

choco install googlechrome -y
choco install git -y
choco install notepadplusplus -y
choco install sql-server-management-studio -y

(I think the -y makes them run without a prompt.)

The script should check if Chocolatey is installed and if not, run the install script. Then it should loop through a list of package names and silently install them.

  • How do I detect if Chocolatey is already installed?
  • How do I conditionally run the install command based on that result?
  • How do I loop through a list of packages and run the choco install command on each?

If you have any suggestions on how to accomplish my main goal using other means, please let me know.

Answer

postanote picture postanote · Jan 8, 2018

All of your questions could be answered by looking at the PowerShell help files and Microsoft tech documentation:

(Get-Command -Name Test-Path).Parameters
Get-help -Name Test-Path -Examples
Get-help -Name Test-Path -Full
Get-help -Name Test-Path -Online

For loops

(I think the -y makes them run without a prompt.)

Correct, and it should always be used in scripting.

The script should check if Chocolatey is installed and if not, run the install script. Then it should loop through a list of package names and silently install them.

• How do I detect if Chocolatey is already installed?

Use PowerShell to Quickly Find Installed Software

Use the link above - or there is an environment variable set on installation, ChocolateyInstall which is set to C:\ProgramData\Chocolatey by default.

Test-Path -Path "$env:ProgramData\Chocolatey"

A more deterministic way may be to try

$ChocoInstalled = $false
if (Get-Command choco.exe -ErrorAction SilentlyContinue) {
    $ChocoInstalled = $true
}

# Do something with that for installation

• How do I conditionally run the install command based on that result?

Using an if statement:

If(Test-Path -Path "$env:ProgramData\Chocolatey") {
    DoYourPackageInstallStuff}
Else {
    InstallChoco
    DoYourPackageInstallStuff
}

• How do I loop through a list of packages and run the choco install command on each?

Using a for loop:

$Packages = 'googlechrome', 'git', 'notepadplusplus', 'sql-server-management-studio'

ForEach ($PackageName in $Packages)
{
    choco install $PackageName -y
}

Alternative / Enhancement

Microsoft has a built-in package manager manager called PackageManagement (built into PowerShell v5). You can use it with a ChocolateyGet provider (don't use the prototype Chocolatey provider, it is broken and has security issues) for managing third-party dependencies.

The advantage of PackageManagement is that it also has PowerShellGet for managing PowerShell modules.

Just type..

List all available modules / packages

Find-Module

Find-Module -Name SomeSpecificModuleName(s)

For PowerShell version 3 - 4, you have to download and install PowerShellGet.