Use this helper CLI for switching .NET Core SDK versions

A friend of mine, Herman van der Merwe; recently asked me how one can use the .NET Core CLI to switch to a specific version of the .NET Core SDK. After crawling the web, and the official docs for that matter; I realized that the only proper way to do this is to specify the version inside a global.json file in the same/parent directory. Unfortunately I couldn't find any existing way of doing this via the CLI, so I decided to create one.

According to the official .NET Core CLI docs, you can specify --fx-version, but this is the version of the runtime framework and not the SDK version.

The .NET Core SDK CLI Helpers

What this helper basically does, it manifests itself onto the existing dotnet toolchain and exposes some handy commands for quickly switching SDK versions from within the command line. For example:

> dotnet sdk 1.0.4
Switching .NET Core SDK version to 1.0.4  

You can also get a list of all the installed SDKs by running:

> dotnet sdk list
The installed .NET Core SDKs are:  
1.0.3  
1.0.4  
1.1.0-preview1-005051  
2.0.0-preview1-005977  
2.0.0-preview2-006497  

If you can't remember the exact version of the latest SDK and want to quickly switch to it, simply pass latest as the version:

> dotnet sdk latest
.NET Core SDK version switched to latest version.
2.0.0-preview2-006497  
The .NET Core CLI is quite extensible, allowing one to add their own custom dotnet commands. You can read more on the .NET Core CLI tools extensibility model here.

It even comes with a shorter .net notation which is just an alias for the dotnet command:

> .net new console

is the same as

> dotnet new console

How do I get it?

It's simple really, it's all just a bunch of CMD scripts:

  1. Go grab them at my Github repo here and copy them into a folder location of your choice.
  2. Open up Command Prompt as Administrator and CD into the folder where the scripts are.
  3. Run dotnet .net and you should be good to go.
All this command does, it adds the script folder to the PATH environmental variable. You might need to close and re-open the Command Line for this to take affect

How does it work?

Like I mentioned before, the only proper way to do this at the moment is to use the 'global.json' file. This helper does exactly that underneath the surface. Wherever dotnet sdk [version] or .net sdk [version] is executed, it creates a global.json file at that location.

That's it!

Regardless if this little helper is actually helpful, I will most certainly use it in my daily workflow with .NET Core.