The .NET CLI (Command-Line Interface) is a powerful tool that lets you create, build, run, and manage .NET projects right from your terminal. Whether you’re a beginner or a seasoned developer, mastering the CLI will make you faster, more productive, and able to automate common tasks with ease.
This article will walk you through the most useful .NET CLI commands, explain what they do, and show you how to use them with clear, beginner-friendly examples.
Introduction to the .NET CLI
The .NET CLI is a cross-platform tool that works on Windows, macOS, and Linux. It comes with the .NET SDK and is available as the
dotnet command in your terminal or command prompt.
Setting Up the CLI
- Make sure you have the .NET SDK installed.
- Open your terminal (Command Prompt, PowerShell, or Terminal on macOS/Linux).
- Type
dotnet --versionto check your installation.
|
1 |
dotnet --version |
If you see a version number, you’re ready to go!
Project Management Commands
These commands help you create, build, and run .NET projects.
Create a New Project
|
1 |
dotnet new console -n MyApp |
This creates a new console application in a folder called MyApp.
Build Your Project
|
1 |
dotnet build |
Compiles your code and checks for errors.
Run Your Application
|
1 |
dotnet run |
Builds (if needed) and runs your application.
Restore Dependencies
|
1 |
dotnet restore |
Downloads and installs any NuGet packages your project needs.
Package Management with CLI
You can add, remove, and update NuGet packages directly from the CLI.
Add a Package
|
1 |
dotnet add package Newtonsoft.Json |
Adds the popular Newtonsoft.Json package to your project.
Remove a Package
|
1 |
dotnet remove package Newtonsoft.Json |
Removes a package from your project.
List Installed Packages
|
1 |
dotnet list package |
Shows all NuGet packages currently installed in your project.
Useful Advanced Commands
Run Unit Tests
|
1 |
dotnet test |
Discovers and runs tests in your project (using xUnit, NUnit, or MSTest).
Publish for Deployment
|
1 |
dotnet publish -c Release -o ./publish |
Builds your app for release and puts the output in the ./publish folder—ready for deployment.
Global Tools
|
1 |
dotnet tool install -g dotnet-ef |
Installs a global tool (like Entity Framework CLI) for use in any project.
Troubleshooting and Tips
- Use
dotnet --helpordotnet [command] --helpto see all available options. - Use tab completion in your terminal to speed up typing commands.
- Keep your .NET SDK up to date for the latest features and bug fixes.
- Use
dotnet cleanto remove old build files if you run into strange errors.
Summary
The .NET CLI is your Swiss Army knife for .NET development. With just a few commands, you can create, build, run, test, and deploy your applications—no mouse required! The more you use the CLI, the more productive and confident you’ll become as a .NET developer.
This article is part of our .NET Essentials series. Next up: What is NuGet – learn how to manage dependencies and packages in your .NET projects.