Powershell package management – NuGet, Chocolatey and Co

There is a new feature available for Powershell since the release of Windows 10. It’s a (open source) package management tool called OneGet. It allows o add different package managers (NuGet, chocolatey, …) and install packages from those sources.
That’s really awesome as we know the useful apt-get or as developers in the Microsoft area – the nice NuGet Package manager. Such package management is now available for Powershell! So let’s have a look on the basic features of it and let’s start with NuGet for Powershell.

NuGet for Powershell

This blog post uses Powershell 5.0. The previous versions do not contain the package management, but you can use a preview version of package management for Powershell 3 and 4. I added a short description how to install and how to use it at the end of this blog post

Searching for NuGet package is simple and can be done via:

Find-Module

It will probably ask to install the NuGet provider at first – just type y and you will get a list with tons of modules.

20161103_01_findmodule

We can already see a lot of Azure modules! So since Powershell 5.0 it’s much easier to install them or keep them up to date – all via the package management. If you want to search for a module – e.g. if you want to find modules for Twitter, just type:

Find-Module *twitter*
# or, which gives you a bit more information:
Find-Module *twitter* | fl
20161103_02_findtwittermodule

NuGet Commandlets

The NuGet module for Powershell has the name “PowerShellGet”. We can list all available commandlets via:

Get-Command -Module PowerShellGet
20161103_03_nugetcommandlets

The three main parts of it are in the area of:
Module: Modules are the core part of the feature – you can install/update/uninstall new modules (like Azure.Storage)
Script: There are some scripts available that you can download to your local environment and use them. I’ll show this later in this post
PSRepository: You can add, set or remove repositories. One repository is the PSGallery (Get-PSRepository lists all registered ones). You can also create your own PSRepository and publish your stuff there!

Install NuGet Modules

Basically works via Install-Module. You can find and search a module in one step or just install it if you know the name of the module:

Find-Module RockPaperScissors | Install-Module # throws an error if powershell doesn't run with elevated privileges
Find-Module RockPaperScissors | Install-Module -Scope CurrentUser
# or
Install-Module RockPaperScissors -Scope CurrentUser

# show all installed modules
Get-Module -ListAvailable
20161103_04_installandgetmodules

After the installation, the module must be imported via Import-Module. After that, we can get all commandlets of the module:

Import-Module RockPaperScissors
Find-Command -ModuleName RockPaperScissors # searches in registered repositories
Get-Command -Module Rock* # show commands of imported modules

Seems that Start-RPSGame is the commandlet.

20161103_05_importmodule

Import Scripts

As already mentioned it’s possible to download scripts to your local environment. The following powershell code searches for scripts, installs it to a specific location and runs it:

Find-Script
Install-Script Minesweeper -Scope CurrentUser
Get-InstalledScript
$script = Get-InstalledScript minesweeper
$script.InstalledLocation
cd $script.InstalledLocation
.\minesweeper.ps1

# or you save it to your preferred location
Save-Script minesweeper -Path c:\data\
20161103_06_scriptminesweeper

Package management

NuGet is only one package manager in the package management. So there is a lot more to explore! Let’s see what else is provided by the package management:

Get-Command -Module PackageManagement # show all commandlets
Get-PackageProvider # shows all registered package providers
20161103_07_packagemanager

if you want to see all installed packages, just type
Get-Package
There are a lot of msi and msu packages. So let’s add a new package manager – chocolatey – and use it to install some basic packages:

Find-PackageProvider  # list all available package proviers
Install-PackageProvider chocolatey -Scope CurrentUser

This commandlet lists the available package providers. If we want to get all available packages – just use:
Find-Package
but it will list tons of packages. So you can filter on a package provider or on a name:

Find-Package -Source chocolatey
Find-Package notepad*
Install-Package notepadplusplus
20161103_08_installnpp

Chocolatey is by default not a trusted provider and that’s why you have to confirm the installation. If you don’t want to confirm it every time, make chocolatey a trusted package source:

Set-PackageSource -Name chocolatey -Trusted
Get-PackageSource

That’s really awesome! So I guess my next client setup will be done via Powershell and chocolatey instead of Ninite. I’ll do it via the following script:

Install-PackageProvider chocolatey -Scope CurrentUser
Set-PackageSource -Name chocolatey -Trusted
 
$packages = ( "7zip", "adobereader", "Firefox", "filezilla", "flashplayerplugin",
"foobar2000", "GoogleChrome", "javaruntime", "keepass", "microsoftazurestorageexplorer",
"Opera", "paint.net", "skype", "teamviewer", "todoist", "vlc", "WhatsApp", "fiddler4" )
 
$packages | ForEach { Install-Package $_ }

Updating a package can be done via:
Install-Package foobar2000 -InstallUpdate

Chocolatey

If you have any issues with chocolatey – e.g. if you install Firefox or Chrome, then I recommend to use the choco.exe. Please find a short description about it in this blog post: Chocolatey – Package management for Windows

Package Management for Powershell 3 and 4

Microsoft announced a preview of package management for Powershell 3 & 4 in March 2016: https://blogs.msdn.microsoft.com/powershell/2016/03/08/package-management-preview-march-2016-for-powershell-4-3-is-now-available/. To use it, execute the following steps:

  1. Download the preview from Microsoft Download Center
  2. (Re)start Powershell
  3. Run Import-Module PowerShellGet
    1. If you receive the following error: “Import-Module : File C:\Program Files (x86)\WindowsPowerShell\Modules\PowerShellGet\PSModule.psm1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at”, execute:
    2. Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope CurrentUser
    3. Run Import-Module PowerShellGet once more.
  4. Run Get-Module to check if the module is loaded
  5. Continue at the top of this blog post and enjoy

Categories:

4 Responses

Leave a Reply

Your email address will not be published. Required fields are marked *

About
about armin

Armin Reiter
Azure, Blockchain & IT-Security
Vienna, Austria

Reiter ITS Logo

Cryptix Logo

Legal information