4 minute read

Here a very small item for my own convenience, but I hope it is helpful to others too.

Winget is the command-line package manager for Windows. For the accidental Linux administrator coming to this page, you’re probably very familiar with package managers like apt, or Chocolatey on Windows. I honestly never understood the reasons why, but in the past year (or so) I started to really appreciate the power of package manager(s). And what better product than an actual Microsoft application to start my journey with.

This will not be an exhaustive comparison of the already existing package managers, and Winget. I don’t have a lot of experience with other package managers in general, and Winget just happens to do enough for me.

Installation

Borrowed directly from the website:

Windows Package Manager winget command-line tool is bundled with Windows 11 and modern versions of Windows 10 by default as the App Installer.
If you are running an earlier version of Windows and the App Installer is not installed, you can get App Installer from the Microsoft Store. If it’s already installed, make sure it is updated with the latest version.

So any newer version of Windows wil have Winget pre-installed. You can use the following to check if it’s available for you.

winget --version
v1.4.2161-preview

Using

Let’s assume you have a new Windows machine you want to use for development, you’ll probably need/want git installed. You can use the command

winget search git

This will result in a list of results from different sources, msstore and winget as the most common ones. The results are all packages containing the three letters git. A better search term would be to use the id of a package, in the case of Git it would be git.git (the first for the organization and the second for the application). For example for Visual Studio Code the id is ‘Microsoft.VisualStudioCode’.

At the time of the writing, the latest release of Git is the 2.38.1 version. This is clearly visible in the result the command with the ‘–id’ switch.

winget search --id git.git
Name Id      Version Source
----------------------------
Git  Git.Git 2.38.1  winget

Now that we are sure that we want to install this particular package, the last command will install Git.

winget install --id Git.Git

This will launch the installer and install Git for you. If admin privileges are required, the installation package will request it. So there is no need to run the initial commands in an elevated windows (cmd/PowerShell).

Keeping up-to-date

Installation of packages is only a part of the power of package managers. You’ll also want to keep the installed packages up-to-date. This too has been implemented in Winget using the upgrade command. The first time you might notice an unexpected surprise. This command will check also for updates for packages/application not installed through winget. For instance if you already installed Visual Studio code through the installer and at the time of running the winget upgrade command, you will also see that there is an update available for it.

winget upgrade
Name                                  Id                                    Version    Available     Source
-----------------------------------------------------------------------------------------------------------
Visual Studio Community 2022          Microsoft.VisualStudio.2022.Community 17.3.6     17.4.0        winget
Docker Desktop                        Docker.DockerDesktop                  4.12.0     4.13.1        winget
Oh My Posh version 12.10.0            JanDeDobbeleer.OhMyPosh               12.10.0    12.13.0       winget
Bicep CLI version 0.11.1.770          Microsoft.Bicep                       0.11.1.770 0.12.40.16777 winget
Microsoft .NET Core SDK 3.1.424 (x64) Microsoft.DotNet.SDK.3_1              3.1.424    3.1.425       winget
Microsoft .NET Core SDK 3.1.300 (x64) Microsoft.DotNet.SDK.3_1              < 3.1.419  3.1.425       winget
6 upgrades available.

The command above only shows if there are updates available. Adding the ‘–all’ switch will list the same list and will start installing/upgrading the packages it found.

One of the quirks of Winget is that some packages are available for upgrade. You go through the process of installation/upgrade. It succeeds and immediately afterwards you run the ‘winget upgrade’ command again, to find out the package is still available for upgrade. I really haven’t found any drawbacks of this behavior, besides it will try to install/upgrade the package again. I trust this is resolved over time.
Another thing is the update of the source. For example I know PowerShell 7.3.0 has been released last night (Dutch timezone) and Visual Studio Code already mentioned I can upgrade, but Winget doesn’t know about it yet. Normally it is available the next day. Again, not a big issue for me. Installing it (PowerShell) through other means doesn’t prevent Winget doing it the next time.

New machine

Now for the latest trick I just put in some effort. To make it easier to have a new workstation ready for use, you’ll need your favorite tools at your capable hands. While you could install your favorites and required packages one-by-one, you probably are way too lazy to do that. Winget has a solution for it, the import command.

With the import command, you can load a predefined list of package in a json file and have them installed. For example the following json file called ‘winget.json’, will install the listed packages (if not already installed.)

{
	"$schema" : "https://aka.ms/winget-packages.schema.2.0.json",
	"CreationDate" : "2022-11-09T18:47:42.503-00:00",
	"Sources" : 
	[
		{
			"Packages" : 
			[
				{
					"PackageIdentifier" : "Git.Git"
				},
				{
					"PackageIdentifier" : "Microsoft.WindowsTerminal"
				},
				{
					"PackageIdentifier" : "JanDeDobbeleer.OhMyPosh"
				},
				{
					"PackageIdentifier" : "Microsoft.AzureDataStudio"
				},
				{
					"PackageIdentifier" : "Microsoft.VisualStudioCode"
				},
				{
					"PackageIdentifier" : "Microsoft.PowerShell"
				}
			],
			"SourceDetails" : 
			{
				"Argument" : "https://cdn.winget.microsoft.com/cache",
				"Identifier" : "Microsoft.Winget.Source_8wekyb3d8bbwe",
				"Name" : "winget",
				"Type" : "Microsoft.PreIndexed.Package"
			}
		}
	],
	"WinGetVersion" : "1.4.2161-preview"
}

You can reference the file with the following command. The path to the file of course must exist.

winget import --import-file .\winget.json

This will have your new workstation ready with the tools you love ready in a blink of an eye.

To generate the above file, you can use the ‘export’ command.

winget export --output winget.json

Conclusion

While there are still some things that are not perfect, it is already way better compared to the point and click installations that we are used to in a Windows environment. Please use it to your advantage.