Bootstrapping Windows and PowerShell v5

Windows 10 Technical Preview and the Windows Management Framework (WMF) v5 have both brought with them PowerShell v5, which has so many updates to it that it is mind-blowing. The one that got me excited recently is OneGet – a new way of downloading and installing packages. I know – Linux users have had this forever. However, so have Windows users – it just wasn’t built in. The package manager of choice for many was Chocolatey. In my past, the process of bootstrapping a new machine was:

  1. Install the Operating System.
  2. Download and install Windows PowerShell v3 (if needed).
  3. Set the PowerShell Execution Policy to RemoteSigned.
  4. Go to outlook.com and download my basicinstaller.ps1 script.
  5. Run a “basicinstaller.ps1” script.
  6. Go away for a few hours.

This was a good process. With Windows 10 Preview edition, it’s this:

  1. Install the Operating System.
  2. Go to outlook.com and download my basicinstaller.ps1 script.
  3. Run the basicinstaller.ps1 script.
  4. Go away for a few hours.

Windows 10 Preview comes with WMF v5 so I don’t need to mess around with installing or updating PowerShell or setting the execution policy any more. There are a few different commands, however. In the past I needed to download the Chocolatey Installer and run it. I also needed to install one of the packages (called binroot) right at the start. Here is my old version:

iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))
choco install binroot -y

In the new world I need to register the chocolatey package provider:

Get-PackageProvider chocolatey -ForceBootstrap

There is a similar difference when installing packages. In the old version, I would run the choco command:

choco install $Package -y

In the new version, I run Install-Package:

Install-Package -Verbose -Force $Package

I modified my basicinstaller.ps1 to handle both situations (although I am starting to use WMF v5 more and more now). Here is the very simple script.

#
# Installer for a new machine
#
function MyInstaller($Package)
{
    if ($PSVersionTable.PSVersion.Major -eq 5) {
        Install-Package -Verbose -Force $Package
    } else {
        choco install $Package -y
    }
}

if ($PSVersionTable.PSVersion.Major -eq 5) {
    Get-PackageProvider chocolatey -ForceBootstrap -ErrorAction SilentlyContinue
} else {
    iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))
    choco install binroot -y
}

# Main Toolset for any machine
MyInstaller 7zip
MyInstaller winscp
MyInstaller putty
MyInstaller wget

7zip did require administrator elevation (so run it from an elevated prompt). Aside from that, things went really well. Just add your standard packages to the bottom of the script.