dotnet new failing with Error: Invalid parameter(s)

If you are using Visual Studio Code and you want to add a new project to your workspace, the easiest thing to do is something like –

dotnet new xunit -f netcoreapp2.1

But you may get an error –

Error: Invalid parameter(s):
   -f netcoreapp2.1
      'netcoreapp2.1' is not a valid value for -f (Framework)

Here’s how to solve it.

Run –

dotnet --info

You will get a list of installed frameworks, your list will be different than mine –

This will show the list of frameworks installed 
.NET Core SDKs installed:
  2.1.202 [C:\Program Files\dotnet\sdk]
  2.1.402 [C:\Program Files\dotnet\sdk]
  2.1.503 [C:\Program Files\dotnet\sdk]
  2.2.203 [C:\Program Files\dotnet\sdk]

Choose a version of the framework that you want, in my case I’ll use 2.1.503

Run this –

dotnet new globaljson --sdk-version 2.1.503

Now run –

dotnet new xunit -f netcoreapp2.1

and the new project will be created.