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.
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
NuGet Commandlets
The NuGet module for Powershell has the name “PowerShellGet”. We can list all available commandlets via:
Get-Command -Module PowerShellGet
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
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.
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\
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
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
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:
- Download the preview from Microsoft Download Center
- (Re)start Powershell
- Run Import-Module PowerShellGet
- 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:
- Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope CurrentUser
- Run Import-Module PowerShellGet once more.
- Run Get-Module to check if the module is loaded
- Continue at the top of this blog post and enjoy
4 Responses
Are you okay with us adding this to our next “news from the community” post on http://blog.myget.org?
Hi Maarten,
sure, I would be very happy if you add it!
Cool! Will get it in there +/- end of month.
[…] Powershell package management – NuGet, Chocolatey and Co […]